Javascript 更新对象列表中的特定属性
我有一个Javascript 更新对象列表中的特定属性,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我有一个dataArray对象列表 我想将TextInput与此列表中的特定对象关联 文本的每次更改也必须更改状态中的数据数组 如何正确地做?下面的代码不起作用 export default class MyClass extends Component { constructor(props) { super(props); this.state = { dataArray: this.props.dataArray }; } _render
dataArray
对象列表
我想将TextInput与此列表中的特定对象关联
文本的每次更改也必须更改状态中的数据数组
如何正确地做?下面的代码不起作用
export default class MyClass extends Component {
constructor(props) {
super(props);
this.state = {
dataArray: this.props.dataArray
};
}
_renderContent = section => {
let arrayIdx = this.state.dataArray.findIndex(
x => x.title == section.title
);
return (
<TextInput
value={this.state.dataArray[arrayIdx].content}
onChangeText={con =>
this.setState({
dataArray: update(this.state.dataArray, {
arrayIdx: { content: { $set: con } }
})
})
}
/>
);
};
render() {
return (
<Container>
<Content padder>
<Accordion
dataArray={this.state.dataArray}
renderContent={this._renderContent}
/>
</Content>
</Container>
);
}
}
导出默认类MyClass扩展组件{
建造师(道具){
超级(道具);
此.state={
dataArray:this.props.dataArray
};
}
_renderContent=section=>{
设arrayIdx=this.state.dataArray.findIndex(
x=>x.title==section.title
);
返回(
这是我的国家({
dataArray:update(this.state.dataArray{
arrayIdx:{content:{$set:con}}
})
})
}
/>
);
};
render(){
返回(
);
}
}
问题是您实际上是在更新arrayIdx
,而不是它所指的索引。您需要使用类似的[arrayIdx]
[arrayIdx]: { content: { $set: con } }
它不起作用是什么意思?你有什么有用的错误吗?你的电脑开始烧坏了吗?首先我建议为手风琴节制作一个外部组件。我的意思是,\u renderContent
应该是一个外部组件,是文本输入的包装,然后导入到这里。其次,onChangeText
也应该是TextInputWrapper
组件中的一个单独函数,而不是内联函数。是的,这需要一些重构,但是您的代码会更干净、更容易理解,并且可能会解决您的问题(我们不知道是什么问题)