【发布时间】:2020-09-08 13:03:24
【问题描述】:
当我点击<a> 标记时,我正在尝试用<input> 标记值更改this.state.id 的值,如下所示:
{this.state.json[1].map(i => (
<tr>
<td>{i.cin}</td>
<td>{i.nom}</td>
<td>{i.prenom}</td>
<td>
<a onClick={() => this.state.click(i.id)} >
<input value={i.id} hidden></input>
Create a New Folder
</a>
</td>
</tr>
))}
但控制台显示this.setState is not a function。这是我的组件:
constructor(props)
{
super(props);
this.state = {
json:JSON.parse(props.data),//data received from a laravel controller used to
implement the page.
objet: '',
id: '',
click: function(id){
var a = id;//a is an integer
alert('hitting waypoint 1');//clear hit
this.setState({//error
id: a,
}).bind(this);
alert('hitting waypoint 2');//not alerting
alert("id = " + this.state.id)//not alerting
}
};
this.onChangeValue = this.onChangeValue.bind(this);
this.onSubmitButton = this.onSubmitButton.bind(this);
}
【问题讨论】:
-
click不属于该州。已经有一个解释如何修复的答案,所以我只想说:状态是随时间变化的值。除非您基于组件状态出于某种原因更新点击功能,否则它不应该存在。另外,在state对象中绑定它会使this引用状态对象,而不是类。
标签: javascript reactjs state setstate