Javascript 如何使用参数更改组件的本地状态?
我在尝试使用参数更改组件的本地状态时遇到一些问题 这是我的代码:Javascript 如何使用参数更改组件的本地状态?,javascript,reactjs,ecmascript-6,Javascript,Reactjs,Ecmascript 6,我在尝试使用参数更改组件的本地状态时遇到一些问题 这是我的代码: class App extends Component { state = { startup: '', investor: '', realState: '', ecoProject: '', }; handleChangeCardSelection = async (e, stateValue, otherCards) => { const currentCard =
class App extends Component {
state = {
startup: '',
investor: '',
realState: '',
ecoProject: '',
};
handleChangeCardSelection = async (e, stateValue, otherCards) => {
const currentCard = await e.currentTarget.id;
this.setState({
[stateValue]: currentCard,
[otherCards]: '',
});
};
render() {
const { startup, investor, realState, ecoProject } = this.state;
return(
<Card
id="investor"
onClick={e => this.handleChangeCardSelection(e, [investor])} />
)
}
}
现在,我从单击调用的函数中得到的是以下状态:
"": "investor"
ecoProject: ""
investor: ""
realState: ""
startup: ""
undefined: ""
我缺少什么?将
[investor]
更改为investor
onClick={e => this.handleChangeCardSelection(e, investor)} />
将
[investor]
更改为investor
onClick={e => this.handleChangeCardSelection(e, investor)} />
您没有正确地将参数传递给函数。在传递给函数时,不需要将参数包装在
[]
中
<Card
id="investor"
onClick={e => this.handleChangeCardSelection(e, investor)} />
this.handleChangeCardSelection(e,投资者)}/>
您没有正确地将参数传递给函数。在传递给函数时,不需要将参数包装在[]
中
<Card
id="investor"
onClick={e => this.handleChangeCardSelection(e, investor)} />
this.handleChangeCardSelection(e,投资者)}/>