简体   繁体   English

在反应钩子中使一个为真,而其他人则反对为假

[英]Make one true while others in object to false in react hooks

What is the best to make (option 1: true ) and at the same time need to change others state to false什么是最好的(选项 1: true ),同时需要将其他状态更改为 false

the same thing needs to do with other states too , (option2,option3.....)其他状态也需要做同样的事情,(option2,option3.....)

const [state, setState] = useState({
        option1: false,
        option2: false,
        option3: false,
        option4: false,
    });

return (
    <div>
        <input
            type="checkbox"
            onClick={() => setState(state => ({ ...state, option1: true }))}
        />

        <input
            type="checkbox"
            onClick={() => setState(state => ({ ...state, option2: true }))}
        />

        <input
            type="checkbox"
            onClick={() => setState(state => ({ ...state, option3: true }))}
        />
    </div>
)

} }

If your state only consist options variables then如果您的状态仅包含options变量,则

const initialState = {
        option1: false,
        option2: false,
        option3: false,
        option4: false,
    }
const [state, setState] = useState(initialState);

return (
    <div>
        <input
            type="checkbox"
            onClick={() => setState(state => ({ ...initialState, option1: true }))}
        />

        <input
            type="checkbox"
            onClick={() => setState(state => ({ ...initialState, option2: true }))}
        />

        <input
            type="checkbox"
            onClick={() => setState(state => ({ ...initialState, option3: true }))}
        />
    </div>
)

I was writing the exact thing as Prakash's answer... Seems to be the simplest way...我写的正是普拉卡什的答案......似乎是最简单的方法......

although I like to write my constants with caps so INITIAL_STATE rather than initialState #personalPreferences虽然我喜欢用大写字母来写我的常量,所以INITIAL_STATE而不是initialState #personalPreferences

I'm not entirely sure why you wouldn't want to use a radio button to do this but heres some code which works for your usecase.我不完全确定为什么您不想使用单选按钮来执行此操作,但这里有一些适用于您的用例的代码。

import React, { useState } from "react";

const initialState = {
  option1: false,
  option2: false,
  option3: false,
  option4: false
};

export default function Hello({ name }) {
  const [formState, setFormState] = useState(initialState);

  function updateForm(option) {
    const newState = { ...initialState }; // create new state object from initialState, spread the initialState to keep it immutable
    newState[option] = true; // set the selected option to true
    setFormState(newState); // update state with new state
  }

  return (
    <div>
      <input
        type="checkbox"
        onClick={() => updateForm("option1")}
        checked={formState.option1}
      />

      <input
        type="checkbox"
        onClick={() => updateForm("option2")}
        checked={formState.option2}
      />

      <input
        type="checkbox"
        onClick={() => updateForm("option3")}
        checked={formState.option3}
      />

      <input
        type="checkbox"
        onClick={() => updateForm("option4")}
        checked={formState.option4}
      />
    </div>
  );
}

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

相关问题 反应:如果 1 state 为真,则将其他更改为假 - React: if 1 state is true, change others to false 将数组中 object 的属性设置为 true,其他设置为 false - Set attribute of the object in array to true but others to false JavaScript对象-查找一个属性-&gt;更改为true,将其他所有属性更改为false - JavaScript Object - Find one property -> change to true and change all others to false React - 如何在 React-Hooks 中隐藏其他元素时显示一个元素 - React - How to show an element while hiding the others in React-Hooks 在redux中将一个对象的值更改为true或false - changing the value of one object to true or false in redux 每当 React 中的某个属性设置为 true 时,我如何将 object 属性切换为 false? - How would I toggle object properties to false, whenever one of the properties is set to true in React? React hooks useMemo, false 作为依赖 - React hooks useMemo, false as dependency useReducer - 只强制一个真值而不明确地将其他值设置为假? - useReducer - Enforce only one true value without explicitly setting others false? 检查 object 数组中的所有值是否等于 false 并在 react 中返回 true - Check if all the values in array of object is equal to false and return true in react 对象存在或对/错 - Object present or true/false
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM