Javascript 更新React Js中的对象数组
我想在Javascript 更新React Js中的对象数组,javascript,reactjs,Javascript,Reactjs,我想在onChange事件中更新问题名称。但问题是,如果我只改变一个问题,每个问题都会改变。如何解决它 class QuestionCreate extends React.Component { constructor(props){ super(props); this.state = { datas: [], default_question: { isNew: true
onChange
事件中更新问题名称。但问题是,如果我只改变一个问题,每个问题都会改变。如何解决它
class QuestionCreate extends React.Component {
constructor(props){
super(props);
this.state = {
datas: [],
default_question: {
isNew: true,
question: {
name: 'Click to write the question text',
answer_options: [
{name: 'click to write choice 1', 'choice': true},
{name: 'click to write choice 1', 'choice': true},
{name: 'click to write choice 1', 'choice': true},
]
}
}
}
}
onChangeQuestion(qustions, index, event){
let all_data = this.state.datas;
let currentQuestion = all_data[index].question
all_data[index]['question']['name'] = event.target.value;
console.log(all_data[index]['question']['name'])
this.setState({datas: all_data})
}
displayRow(){
let rowData = this.state.datas.map( (data, index) =>{
console.log("this is data:", data, index);
return(
<div className="well" key={ index }>
<h3>Q. <input type="text" onChange={ this.onChangeQuestion.bind(this, data, index) } value={ data.question.name } /></h3>
<ul>
<li>option</li>
</ul>
</div>
)
})
return rowData;
}
classquestioncreate.Component{
建造师(道具){
超级(道具);
此.state={
数据:[],
默认问题:{
是的,
问题:{
名称:“单击以编写问题文本”,
答案:[
{name:'单击写入选项1','choice':true},
{name:'单击写入选项1','choice':true},
{name:'单击写入选项1','choice':true},
]
}
}
}
}
变更问题(问题、索引、事件){
让所有_data=this.state.datas;
让currentQuestion=all_数据[索引]。问题
所有_数据[索引]['question']['name']=event.target.value;
log(所有_数据[索引]['question']['name'])
this.setState({datas:all_data})
}
displayRow(){
让rowData=this.state.datas.map((数据,索引)=>{
log(“这是数据:”,数据,索引);
返回(
Q
- 选择权
)
})
返回行数据;
}
你正在直接改变你的状态
let all_data = this.state.datas;
let currentQuestion = all_data[index].question
all_data[index]['question']['name'] = event.target.value;
使用:
这些问题也可能有帮助
- 你正在直接改变你的状态
let all_data = this.state.datas;
let currentQuestion = all_data[index].question
all_data[index]['question']['name'] = event.target.value;
使用:
这些问题也可能有帮助
onChangeQuestion
功能后,它正在工作
onChangeQuestion(qustions, index, event){
let all_data = JSON.parse(JSON.stringify(this.state.datas));
let currentQuestion = all_data[index].question
all_data[index]['question']['name'] = event.target.value;
this.setState({datas: all_data});
}
在编辑
onChangeQuestion
函数后,它将工作
onChangeQuestion(qustions, index, event){
let all_data = JSON.parse(JSON.stringify(this.state.datas));
let currentQuestion = all_data[index].question
all_data[index]['question']['name'] = event.target.value;
this.setState({datas: all_data});
}
你能把这段代码放在Fiddle、Codepen或Plunker中吗?请把你的初始数据和操作后得到的数据放在js Fiddle中。我已经把它放在js Fiddle中了。你能把这段代码放在Fiddle、Codepen或Plunker中吗?请把你的初始数据和操作后得到的数据放在js Fiddle中。你正在改变
这个状态
使用上述语句。这不好。您可以阅读以更好地理解问题。一定要询问您是否还有任何疑问ChangeQuestion(问题、索引、事件){let all_data=This.state.datas;let currentQuestion=all_data[index]。问题let newQues=Object.assign({},currentQuestion,{name:event.target.value})this.setState({datas:all_data})需要这行吗?this.setState({datas:all_data})在let all_data=this.state.datas;
行中,您正在为this.state.datas
分配一个引用。因此,当您更改all_data
时,您将更改this.state
。为了避免这种情况,您可以执行let all_data=JSON.parse(JSON.stringify(this.state.datas))
创建this.state.datas
的副本。您正在使用上述语句更改this.state
。这不好。您可以阅读以更好地理解问题。一定要询问您是否还有任何疑问ChangeQuestion(问题、索引、事件){let all_data=this.state.datas;let currentQuestion=all_data[index].question let newQues=Object.assign({},currentQuestion,{name:event.target.value})this.setState({datas:all_data})是否需要此行?this.setState({datas:all_data})在let all_data=this.state.datas;
行中,您正在为this.state.datas
分配一个引用。因此,当您更改all_data
时,您将更改this.state
。为了避免这种情况,您可以执行let all_data=JSON.parse(JSON.stringify(this.state.datas));
创建此.state.data的副本。