简体   繁体   English

使用 Reactjs 使用道具取消选中复选框和单选按钮

[英]uncheck checkbox and radio button using Reactjs using props

i want to uncheck my checkbox but it does uncheck but just after unchecking it gives error我想取消选中我的复选框,但它确实取消了选中,但在取消选中后它给出了错误

    console.log()

    var x = document.getElementsByName("checkbox");
    for(let i=0; i<=x.length; i++) {
      x[i].Checked = false;
      }   
  }

<button onClick={() => clearClickHandler()}>Clear all filter</button>

<input type="checkbox" name="checkbox" defaultChecked={false} onChange={() => sortDispatch({payload: "INCLUDE_OUT_OF_STOCK"})}></input>
          Include out of stock
        </label>

Issue问题

Direct DOM mutations are anti-pattern in React and even if it wasn't throwing an error since the change happens outside of React, React won't be aware of the change and won't rerender the UI.直接 DOM 突变在 React 中是反模式,即使由于更改发生在 React之外而没有引发错误,React 也不会意识到更改并且不会重新渲染 UI。

var x = document.getElementsByName("checkbox");
for(let i = 0; i <= x.length; i++) {
  x[i].Checked = false; // <-- mutation!!! should probably also be `.checked`
}

Solution解决方案

You can use Fully uncontrolled component with a key .您可以将完全不受控制的组件与 key 一起使用

In order to reset the value when moving to a different item (as in our password manager scenario), we can use the special React attribute called key .为了在移动到不同的项目时重置值(如在我们的密码管理器场景中),我们可以使用名为key的特殊 React 属性。 When a key changes, React will create a new component instance rather than update the current one .当一个key改变时,React 将创建一个新的组件实例而不是更新当前的 Keys are usually used for dynamic lists but are also useful here.键通常用于动态列表,但在这里也很有用。

Here's an example demo applying using a React key against a set of inputs.这是一个使用 React 键对一组输入应用的示例演示。

function App() {
  const [key, setKey] = useState(0);

  const clearClickHandler = () => setKey((k) => k + 1);

  return (
    <div className="App">
      <Fragment key={key}> // <-- React key updates will "reset" inputs
        <label>
          <input type="checkbox" name="checkbox1" defaultChecked={false} />
          Include out of stock
        </label>
        <label>
          <input type="checkbox" name="checkbox2" defaultChecked={false} />
          Include out of stock
        </label>
        <label>
          <input type="checkbox" name="checkbox3" defaultChecked={false} />
          Include out of stock
        </label>
      </Fragment>

      <div>
        <button onClick={clearClickHandler}>Clear all filter</button>
      </div>
    </div>
  );
}

编辑 uncheck-checkbox-and-radio-button-using-reactjs-using-props

You can create refs attached to the elements you want to check and uncheck programmatically.您可以创建附加到要以编程方式检查和取消选中的元素的引用。

Then have a functions that manages the unchecking然后有一个管理取消选中的功能

uncheckAll() {
    // the length of the array below should
   //  depend on the numbers of ref you have

   [1].forEach((refId) => this.refs[`ref_${refId}`].unCheck)
}

Note: this assumes your components are based on class注意:这假设您的组件基于 class

<input type="checkbox" ref="check_1" defaultChecked={false} onChange={() => sortDispatch({payload: "INCLUDE_OUT_OF_STOCK"})}></input>
      Include out of stock
    </label>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM