简体   繁体   English

反应 js state 值始终为 false

[英]react js state value always false

hello im trying to make a array that contain a objects and i want to map this array into two tables:thisis stricture of array:你好,我正在尝试制作一个包含对象的数组,我想将这个数组 map 放入两个表中:这是数组的结构:

0
: 
{title: 'uml', agreement: false}
1
: 
{title: 'react', agreement: false}
2
: 
{title: 'laravel', agreement: false}
length
: 
3
[[Prototype]]
: 
Array(0) 

and i have a checkbox that make agreement true or false.我有一个复选框,可以确定协议的真假。 but the problem is everytime the agreement is false.但问题是每次协议都是假的。 i want to send objects to first table if is true and to seconde table if is false but everytime is send to first table with false agreement.如果为真,我想将对象发送到第一个表;如果为假,我想将对象发送到第二个表,但每次都以错误的协议发送到第一个表。 this is all of code: some functions is just to show the values of states这就是所有代码:一些函数只是为了显示状态的值

import './App.css';
import { useRef, useState } from 'react';

function App() {

  const [modules,setModules]=useState([])
  const [agreement,setAgreement]=useState()
  const [title,setTitle]=useState()
  const checkbox=useRef()

  function handlecheck(){

    setAgreement(checkbox.current.checked)
    
  }
  function handlechange(event){
    setTitle(event.target.value)

  }
  function ajouter(){
    setModules([...modules,{title,agreement}])
    
  }
  function affich(){
    return console.log(modules)
  }


  return (
    <div className="App">
      <section class="container cd-table-container">
        <h2 class="cd-title">Insert Table Record:</h2>
        <input onChange={(event)=>handlechange(event)} type="text" class="cd-search table-filter" data-table="order-table" placeholder="module name" />
        <button className='ajouter' onClick={()=>ajouter()}  >Ajouter</button>
        <button className='ajouter' onClick={()=>affich()}  >affich</button>
        <input type={"checkbox"} ref={checkbox}   onChange={(event)=>handlecheck(event)} />
        <table class="cd-table table">
          <thead>
            <tr>
              <th>modules regionaux</th>
            </tr>
          </thead>

          <tbody>
            {
              modules.map((elm,index)=>{
                if(elm.agreement=true){
                  return (<tr>
                    <td>{elm.title}</td>
                  </tr>)
                }
              })
            }
          </tbody>
        </table>
        <br></br>
        <table class="cd-table table">
          <thead>
            <tr>
              <th>modules non regionaux</th>
            </tr>
          </thead>

          <tbody>
          {
              modules.map((elm,index)=>{
                if(elm.agreement=false){
                  return (<tr>
                    <td>{elm.title}</td>
                  </tr>)
                }
              })
            }
          </tbody>
        </table>
      </section>
    </div>
  );
}

export default App;

so first you don't have to use (useRef) hock just use (useState) will be better, second you were using the if statement with the assign operator sign you should use the double equal sign or the triple sign this the full code所以首先你不必使用 (useRef) hock 只使用 (useState) 会更好,其次你使用带有赋值运算符符号的 if 语句你应该使用双等号或三重符号这是完整的代码

 import './App.css'; import { useRef, useState } from 'react'; function App() { const [modules, setModules] = useState([]) const [agreement, setAgreement] = useState() const [title, setTitle] = useState() function handlecheck(e) { setAgreement(e) } function handlechange(event) { setTitle(event.target.value) } function ajouter() { setModules([...modules, { title, agreement }]) } function affich() { return console.log(modules) } return ( <div className="App"> <section class="container cd-table-container"> <h2 class="cd-title">Insert Table Record:</h2> <input onChange={(event) => handlechange(event)} type="text" class="cd-search table-filter" data.table="order-table" placeholder="module name" /> <button className='ajouter' onClick={() => ajouter()} >Ajouter</button> <button className='ajouter' onClick={() => affich()} >affich</button> <input type={"checkbox"} onChange={(event) => handlecheck(event.target.checked)} /> <table class="cd-table table"> <thead> <tr> <th>modules regionaux</th> </tr> </thead> <tbody> { modules.map((elm, index) => { if (elm.agreement === true) { return (<tr> <td>{elm.title}</td> </tr>) } }) } </tbody> </table> <br></br> <table class="cd-table table"> <thead> <tr> <th>modules non regionaux</th> </tr> </thead> <tbody> { modules.map((elm, index) => { if (elm.agreement === false) { return (<tr> <td>{elm.title}</td> </tr>) } }) } </tbody> </table> </section> </div> ); } export default App;

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

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