react-native - 如何输入图像链接并使其显示在下面的黄色视图中?
问题描述
class Newsfeed extends React.Component{
constructor(props){
super(props)
this.state = {
text: ''
}
};
render(){
return (
<View>
<Text style={{fontSize: 50}}>Junior Facebook</Text>
<View style={{flex: 1, flexDirection: "column"}} />
<View style={{top: 20, marginLeft: 0, width: 300, height: 180, backgroundColor: "lightblue"}}>
<TextInput
style={{
height: 150,
borderStyle: "solid",
borderWidth: 2,
fontSize: 30
}}
placeholder="New Post"
onChangeText={(text)=>this.setState({text})}
value={this.state.text}
/>
<TouchableOpacity
style={{ backgroundColor: "green", marginLeft: 220, width: 80, height: 30 }}
>
<Text style={{fontSize: 20}}>Enter</Text>
</TouchableOpacity>
</View>
<View style={{marginTop: 30, marginLeft: 0, width: 300, height: 180, backgroundColor: "pink"}} >
<TouchableOpacity style={{width: 65, height: 45, marginLeft: 260, marginTop: 160}}><Text>Share</Text></TouchableOpacity>
<TouchableOpacity style={{width: 65, height: 45, marginLeft: 230, marginTop: -45}}><Text>Like</Text></TouchableOpacity>
</View>
<View style={{marginTop: 10, marginLeft: 0, width: 300, height: 130, backgroundColor: "yellow"}} >
<Image source={{uri: this.state.text}} style={{width:200, height: 90}} />
</View>
</View>
)
}
}
以上是我的代码,目前,当我输入图片链接时,下面的黄色视图中会出现一张图片。我希望在我输入图像链接并单击“Enter”按钮时出现我的图像。另外,我希望我以前的图像保留在黄色框中。我该怎么做?
谢谢
解决方案
您需要在状态中有一个链接数组,并通过将数组元素映射到图像来显示它。代码是这样的
class Newsfeed extends React.Component {
constructor(props) {
super(props)
this.state = {
text: '',
images: []
}
};
render() {
return (
<View>
<Text style={{ fontSize: 50 }}>Junior Facebook</Text>
<View style={{ flex: 1, flexDirection: "column" }} />
<View style={{ top: 20, marginLeft: 0, width: 300, height: 180, backgroundColor: "lightblue" }}>
<TextInput
style={{
height: 150,
borderStyle: "solid",
borderWidth: 2,
fontSize: 30
}}
placeholder="New Post"
onChangeText={(text) => this.setState({ text })}
value={this.state.text}
/>
<TouchableOpacity
style={{ backgroundColor: "green", marginLeft: 220, width: 80, height: 30 }}
onPress={() => {
this.setState({ images: [...this.state.images, this.state.text] });
}}
>
<Text style={{ fontSize: 20 }}>Enter</Text>
</TouchableOpacity>
</View>
<View style={{ marginTop: 30, marginLeft: 0, width: 300, height: 180, backgroundColor: "pink" }} >
<TouchableOpacity style={{ width: 65, height: 45, marginLeft: 260, marginTop: 160 }}><Text>Share</Text></TouchableOpacity>
<TouchableOpacity style={{ width: 65, height: 45, marginLeft: 230, marginTop: -45 }}><Text>Like</Text></TouchableOpacity>
</View>
<View style={{ marginTop: 10, marginLeft: 0, width: 300, height: 130, backgroundColor: "yellow" }} >
{this.state.images.map(link => <Image source={{ uri: link }} style={{ width: 200, height: 90 }} />)}
</View>
</View>
)
}
}
推荐阅读
- c# - C# UWP 使用 KeyDown 仅更改一次图像
- c++ - 为什么当我反转 LinkList 时出现分段错误(核心转储)?
- gnuradio - 使用 RTL-SDR 加密狗的 Z-Wave 的 GNURadio Companion Blocks
- ruby-on-rails - Rails ActiveRecord mysql2 适配器,默认使用 PreparedStatement
- python-3.x - 具有异步函数的 Python 多处理
- python - 如何使用python将数据放入csv文件?
- android - 如何在滑动回收站视图项目时创建执行操作。就像在 gmail 应用程序中滑动删除选项一样。但不应删除回收站视图项
- java - 在调用 getter 方法时获取 NullPointerException
- ms-word - officejs:无法将超链接文本的下划线属性设置为 false
- c - 拆分 char[] 并将值存储在不同的数组中 C