react-native Image之踩坑系列
- 废话不多说直接进入主题
react-native Image 变量加载本地图片
之前一直不能够在项目中本地用变量去加载图片,要么一张一张的写,要么就直接废了。这两天好好研究了下,总所周知,在rn下是不允许进行对Image组件中加载进行字符串拼接的
下面就来看看怎么搞定这个问题
代码块
代码块语法遵循标准markdown代码,例如:
export default { //放在一个img_arr.js文件中
png1:require('./img/icona.png'),
png2:require('./img/iconb.png'),
png3:require('./img/iconc.png'),
png4:require('./img/icond.png')
};
import img_arr from './img_arr';//引入文件
onstructor(){ //设置state属性
super()
this.state={
T_img:''
};
}
<View>//根据输入的text文本的值来改变图片
<TextInput
onChangeText={(text) => { this.setState({ T_img: text }) }}
/>
<Image key={i} source={img_arr['png'+this.state.T_img]} style={{width:width,height:500,backgroundColor:"red"}} />
</View>
这样就可以啦!
流程图: