简体   繁体   English

反应:如果 1 state 为真,则将其他更改为假

[英]React: if 1 state is true, change others to false

I have 4 buttons, for each of which I applied the following functions:我有 4 个按钮,我为每个按钮应用了以下功能:

    handleStip() {
        this.setState({isStipOpen: !this.state.isStipOpen, isBookmarkOpen: false, isDonateOpen: false});
    }

    handleBookmark() {
        this.setState({isBookmarkOpen: !this.state.isBookmarkOpen, isStipOpen: false, isDonateOpen: false});
    }

   handleDonate(){
        this.setState({isDonateOpen: !this.state.isDonateOpen, isStipOpen: false, isBookmarkOpen: false});

    }

I have no problems with the functionality.我的功能没有问题。 The main problem is size.主要问题是尺寸。 For example, now I need to add 2 more buttons, again copying and pasting all these states.例如,现在我需要再添加 2 个按钮,再次复制和粘贴所有这些状态。

Is there a more elegant way to solve this problem?有没有更优雅的方法来解决这个问题? Thanks谢谢

How about creating a generic function that can receive the key that you want to toggle.如何创建一个可以接收您想要切换的键的通用 function。 Create a state copy.创建 state 副本。 Iterate all keys and set them to false.迭代所有键并将它们设置为 false。 Set the key in question to toggle state.将相关键设置为切换 state。

handleButtonStateChange = (toggleKey) => {
    const stateCopy = {...this.state};
    const nextValue = !stateCopy[toggleKey];
    Object.keys(stateCopy).forEach(key => stateCopy[key] = false);
    stateCopy[toggleKey] = nextValue;
    this.setState(stateCopy);
}

Usage用法

onChange={() => this.handleButtonStateChange('isBookmarkOpen')}

Maybe you can create function what will create object with false values也许你可以创建 function 什么会创建 object 假值

const prepareData = () => ({
isStipOpen: false
isBookmarkOpen: false,
isDonateOpen: false
});
handleEvent(type) {
        this.setState({...this.prepareData(), [type]: !this.state[type]});
    }

But i do not know you logic, maybe it will not work但我不知道你的逻辑,也许它不会工作

According to comment above we can use type param根据上面的评论,我们可以使用type参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在反应钩子中使一个为真,而其他人则反对为假 - Make one true while others in object to false in react hooks 当我将状态从 true 更改为 false 时,为什么我的模态不会隐藏在反应中? - Why does my modal not hide in react when I change my state from true to false? 在反应中从功能组件传递真/假状态 - Pass true/false state from functional component in react JavaScript对象-查找一个属性->更改为true,将其他所有属性更改为false - JavaScript Object - Find one property -> change to true and change all others to false 将数组中 object 的属性设置为 true,其他设置为 false - Set attribute of the object in array to true but others to false 如果单击复选框,如何将复选框从true更改为false - React how to change checkbox from true to false if checkbox clicked 反应上下文 - 状态不正确 - React context - state not true 仅更新我更改其他人的日期 state 不应更新或更改 ReactJS/React-Hooks - Only update the date I change others state should not update or change ReactJS/React-Hooks 真假单选按钮反应 - True and false radio buttons in react 如何使用 React Hook useEffect 检查状态默认布尔 false 是否更改为 true - How to check if state default boolean false is changing to true with React Hook useEffect
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM