react-native - 我怎样才能让我以前的 TextInput 值留在盒子里反应原生?
问题描述
class Newsfeed extends React.Component {
state = {
TextInputValueHolder: '',
button: '',
posts: []
};
handlePress = () => {
const value = this.state.TextInputValueHolder;
this.setState({ button: value });
}
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={TextInputValueHolder =>
this.setState({ TextInputValueHolder })
}
/>
<TouchableOpacity
onPress={this.handlePress}
style={{ backgroundColor: "green", marginLeft: 220, width: 80, height: 40 }}
>
<Text style={{ fontSize: 24 }}>Enter</Text>
</TouchableOpacity>
</View>
<View style={{ marginTop: 30, marginLeft: 0, width: 300, height: 180, backgroundColor: "pink" }} >
{/* <Text style={{fontSize: 40, marginLeft: 0, marginTop: 0, width: 300, height: 150, borderStyle: "solid", borderWidth: 2}}>{this.state.value}</Text> */}
<Text>{this.state.button}</Text>
<TouchableOpacity style={{ width: 65, height: 45, marginLeft: 260 }}><Text>Share</Text></TouchableOpacity>
<TouchableOpacity style={{ width: 65, height: 45, marginLeft: 220, marginTop: -45 }}><Text>Like</Text></TouchableOpacity>
</View>
</View>
)
}
}
以上是我的代码,使我的 TextInput 值出现在下面的视图组件中。但我希望我以前的 textinput 值即使在新输入之后也保留在框中。就像一个待办事项列表。我该怎么做?
请帮帮我
解决方案
您可以将按钮对象声明为数组而不是字符串,并在视图内循环以显示所有项目
class Newsfeed extends React.Component {
state = {
TextInputValueHolder: '',
button: [], // Initialize your object as an array
posts: []
};
handlePress = () => {
const value = this.state.TextInputValueHolder;
this.setState(prevState => ({ button: [...prevState.button, value] })); // Push the input value in button array
};
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={TextInputValueHolder => this.setState({ TextInputValueHolder })
}
/>
<TouchableOpacity
onPress={this.handlePress}
style={{
backgroundColor: 'green', marginLeft: 220, width: 80, height: 40
}}
>
<Text style={{ fontSize: 24 }}>Enter</Text>
</TouchableOpacity>
</View>
{ // Map your array here to display results
this.state.button.map(item => (
<View style={{
marginTop: 30, marginLeft: 0, width: 300, height: 180, backgroundColor: 'pink'
}}
>
<Text>{item}</Text>
<TouchableOpacity style={{ width: 65, height: 45, marginLeft: 260 }}><Text>Share</Text></TouchableOpacity>
<TouchableOpacity style={{
width: 65, height: 45, marginLeft: 220, marginTop: -45
}}
>
<Text>Like</Text>
</TouchableOpacity>
</View>
))
}
</View>
);
}
}
推荐阅读
- c++ - 共享指针和具有自定义删除器的唯一指针之间的语法差异背后的任何原因
- face-detection - 使用 IR 图像进行人脸检测
- javascript - 使用 .js (JavaScript) 文件使 ipodlinux.org 上的不可点击链接可点击
- jenkins - 强制 UTF8 环境变量 Jenkins
- python - 由于数据长度不等,将嵌套 JSON 列表展平为 Pandas DataFrame 的问题
- python - 字典只返回部分答案(也许是关于缩进)
- c++ - 提升范围和 ADL 查找导致不明确的调用
- url - 屏蔽敏感的 url 查询参数
- ios - 从另一个类swift调用文本字段
- mysql - 从mysql获取数据,耗时近436秒