参考:
icon 来源于阿里巴巴图库
react-native如何动态引用本地图片
Icon.js
export default {avatar1: require('./avatar01.png'),avatar2: require('./avatar02.png'),avatar3: require('./avatar03.png'),avatar4: require('./avatar04.png'),avatar5: require('./avatar05.png'),avatar6: require('./avatar06.png'),avatar7: require('./avatar07.png'),avatar8: require('./avatar08.png'),
};
App5.js:
import React, {Component} from 'react';
import {StyleSheet,View,TextInput,Image,Dimensions,
} from 'react-native';
import Icon from &#39;./icons/Icon&#39;;let {height, width} &#61; Dimensions.get(&#39;window&#39;);export class App5 extends Component {constructor() {super();this.state &#61; {index: &#39;1&#39;,};}render() {return (<View style&#61;{styles.container}><TextInputstyle&#61;{styles.textInput}onChangeText&#61;{(text) &#61;> {this.setState({index: text});}}/><Imagestyle&#61;{styles.image}source&#61;{Icon[&#39;avatar&#39; &#43; this.state.index]}/></View>);}
}const styles &#61; StyleSheet.create({container: {flex: 1,justifyContent: &#39;center&#39;,alignItems: &#39;center&#39;,backgroundColor: &#39;#000000&#39;,},textInput: {width: width - 40,backgroundColor: &#39;#ffffff&#39;,},image: {width: 200,height: 200,marginTop: 20,},
});
效果图&#xff1a;