我在尝试使用参数更改组件的本地状态时遇到一些问题。
这是我的代码:
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])} />
)
}
}
所以,基本上我不想为状态中的每个项目创建一个新函数。
我想要的只是调用 this.handleChangeCardSelection
函数,然后设置我想要的项目的值,并为状态中的其他值设置空字符串。
就像上面的代码一样,我需要执行以下操作:
this.setState({
investor: 'investor' // or any other value to fill the string
startup: '',
realState: '',
ecoProject: '',
})
现在我从点击调用的函数中得到的是这样的状态:
"": "investor"
ecoProject: ""
investor: ""
realState: ""
startup: ""
undefined: ""
我错过了什么?
最佳答案
将[投资者]
更改为投资者
onClick={e => this.handleChangeCardSelection(e, investor)} />
关于javascript - 如何使用参数更改组件本地状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55579429/