我做了两个按钮组件,当我点击一个,我想渲染另一个点击能够禁用(因为他们看起来不一样)。
const match = id;
const checkId = this.state.pokemon.includes(match);
{!checkId || this.state.isDisabled === true ? (
<button
onClick={() =>
this.setState({
isDisabled: true
})
}
>
Get Pokemon
</button>
) : (
<Button disabled/>
)}
</div>
问题是,按钮禁用仅在我刷新时呈现,因为符合检查ID的条件,但我有麻烦,点击后直接切换到禁用按钮
替换为
<button disabled={!checkId || this.state.isDisabled}
onClick={() =>
this.setState({
isDisabled: true
})
}
/>
Get Pokemon
</button>
以下情况似乎有缺陷:
{!checkId | this.state.isDisabled===true?(
我认为条件应改为:
!检查ID | |!这状态isDisabled
,因为从我所看到的,每当点击按钮时,isDisabled
状态被设置为true
,它将条件评估为truthy,因此,falsy案例永远不会执行。
如果您需要执行它一次,每个匹配
,您甚至可以将条件更改为:
<代码>(!检查ID
希望这能有所帮助