How to toggle boolean state of react component?

前端 未结 9 1747
别跟我提以往
别跟我提以往 2021-01-29 23:45

I\'d like to know how to toggle a boolean state of a react component. For instance:

I have boolean state check in the constructor of my component:

const         


        
9条回答
  •  庸人自扰
    2021-01-30 00:28

    Here's an example using hooks (requires React >= 16.8.0)

    // import React, { useState } from 'react';
    const { useState } = React;
    
    function App() {
      const [checked, setChecked] = useState(false);
      const toggleChecked = () => setChecked(value => !value);
      return (
        
      );
    }
    
    const rootElement = document.getElementById("root");
    ReactDOM.render(, rootElement);
    
    
    
    

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题