[英]How to render new row onpress of button
I want to add functionality eg: 'Add New Record'.我想添加功能,例如:“添加新记录”。 I want whenever user clicked on 'Add New' button so new row should be generated below.
我希望每当用户单击“添加新”按钮时,都应该在下面生成新行。 after that I also want the data of every single row.
之后我还想要每一行的数据。 My attached code is not working, I've also attached the error.
我附加的代码不起作用,我还附加了错误。 Please provide me a solution or another trick for it.
请为我提供解决方案或其他技巧。 Thanks.
谢谢。
undefined is not an object (evaluating 'this.state.row.push')
undefined 不是一个对象(评估'this.state.row.push')
constructor(){
super()
this.state = {
rows: []
}
}
index = 0;
_addRow(){
this.state.rows.push(this.index++)
this.setState({ rows: this.state.rows })
}
render(){
let rows = this.state.rows.map((r, i) => {
return <View key={ i } style={[styles.row, CheckIndex(i)]}>
<Text >Row { r }, Index { i }</Text>
</View>
})
return (
<View style={styles.container}>
<TouchableHighlight onPress={ this._addRow } style={styles.button}>
<Text>Add new row</Text>
</TouchableHighlight>
{ rows }
</View>
);
}
This is because React state
is an immutable object.这是因为
React state
是一个不可变对象。 You can't use functions to mutate stuff like push()
append()
etc.您不能使用函数来改变
push()
append()
等内容。
Instead you should create a new instance, and assign that to your state.相反,您应该创建一个新实例,并将其分配给您的状态。
a great way to do this, is to use the spread operator ...
and create a new array using bracket notation一个很好的方法是使用扩展运算符
...
并使用括号表示法创建一个新数组
here is a general example of this could be done这是可以完成的一般示例
addRow = () =>{
const row ={//the data of the new rowobject}
this.setState({rows: [row, ...this.state.rows]})
}
here the new row object, will just be assigned to a new array, and passed into the new state
这里新的行对象,将被分配给一个新的数组,并传递到新的
state
What @baileyhaldwin said about mutating the state, is totally right. @baileyhaldwin 关于改变状态的说法是完全正确的。
I would amend your _addRow
function like this我会像这样修改你的
_addRow
函数
class App extends Component {
constructor() {
super()
this.state = {
rows: []
}
}
_addRow = () => {
let { rows } = this.state
let index = rows.length
rows.push(index++)
this.setState(rows)
}
render() {
const rows = this.state.rows.map((r, i) => {
return (
<View key={ i }>
<Text >Row { r }, Index { i }</Text>
</View>
)
})
return (
<View>
<TouchableHighlight onPress={this._addRow}>
<Text>Add new row</Text>
</TouchableHighlight>
{rows}
</View>
);
}
}
export default App;
You can try the below code, just make a temporary variable and assign the state value to that variable and then push the new value to that after that set the state with that new variable.您可以尝试以下代码,只需创建一个临时变量并将状态值分配给该变量,然后将新值推送到该变量,然后使用该新变量设置状态。
constructor(){
super()
this.state = {
rows: []
}
}
index = 0;
_addRow(){
let temp = [...this.state.rows]
temp.push(this.index++)
this.state.rows.push()
this.setState({ rows: temp })
}
render(){
let rows = this.state.rows.map((r, i) => {
return <View key={i} style={[styles.row, CheckIndex(i)]}>
<Text >Row {r}, Index {i}</Text>
</View>
})
return (
<View style={styles.container}>
<TouchableHighlight onPress={this._addRow} style={styles.button}>
<Text>Add new row</Text>
</TouchableHighlight>
{rows}
</View>
);
}
I hope this helps...Thanks :)我希望这会有所帮助...谢谢:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.