简体   繁体   English

为什么当我尝试在 onchange 处理程序上控制台登录时无法获得正确的状态?

[英]Why I can't get the right state when I try to console log inside on my onchange handler?

I have problem regarding on getting all the state inside of my HandlerInputChange the data already updated into 10 data however when I console it log inside of my handler it shows the default array value which is 5.我在获取 HandlerInputChange 中的所有状态时遇到问题,数据已经更新为 10 个数据,但是当我控制台它登录到我的处理程序中时,它显示了默认数组值 5。

Question: How to get the all the state data inside of my handler?问题:如何获取处理程序中的所有状态数据?

Goal: To get all the state on my handler目标:让我的处理程序获得所有状态

Logs:日志:

日志

My UseState:我的使用状态:

const [data, setData] = useState(defaultArray) 

My Handler:我的处理程序:

     useEffect(() => {
        console.log("data" , data)
    }, [data])
    
    const handleInputChange = (e, i) => {

        const { name, value } = e.target;

        const list = [...data];

        console.log(list);

    }

Add Rows Function:添加行功能:

const AddRows = () => {

    //loop here the keys listed

    
    let initialKey = 0;

    const x = data.slice(-1);

    initialKey = parseInt(x[0]['key']);

    let newArr=[...Array(5)].map((_,i)=>{

        let val=i + 1 +initialKey;

         return {
            key: ""+val+"",
            customer: <select className="form-control" name="customer" onChange={(e,i) => handleInputChange(e,val)}>
                            <option   disabled selected>Select Customer</option>
                            <option  >LEXI INC / joseph</option>
                            <option  >LEXI INC / james</option>
                            <option  >LEXI INC / charlie</option>
                            <option  >LEXI INC / pam</option>
                            <option >LEXI INC / regorio</option>
                        </select>,
            bank: <select className="form-control" name="bank" onChange={(e,i) => handleInputChange(e,val)}>
                        <option   disabled selected>Select Bank</option>
                        <option >BDO</option>
                        <option  >MAY BANK</option>
                        <option  >EAST WEST BANK</option>
                        <option  >UNION BANK</option>
                        <option  >BPI</option>
                    </select>,
            branch: <input className="form-control" placeholder="Branch" name="branch" onChange={(e,i) => handleInputChange(e,val)}/>,
            checkNo: <input type="number" className="form-control" name="checkNo" placeholder="Check No." onChange={(e,i) => handleInputChange(e,val)}/>,
            checkDate: <input type="date" name="checkDate" className="form-control" onChange={(e,i) => handleInputChange(e,val)}/>,
            amount: <input
                        name="amount"
                        className="form-control"
                        placeholder="Amount"
                        onChange={(e,i) => handleInputChange(e,val)} 
                    />,
            remarks: <select className="form-control" name="remarks" onChange={(e,i) => handleInputChange(e,val)}>
                        <option  disabled selected>Select Remarks</option>
                        <option  >Test</option>
                    </select>
         }
    });

    setData([...data, ...newArr]);


}

Button:按钮:

<button type="submit" class="btn btn-danger" onClick={() => AddRows()}>Add Row</button>

Your handleInputChange is not the problem.您的 handleInputChange 不是问题。 Normally, it will be return an array with 10 elements like the variable defaultArray.通常,它将返回一个包含 10 个元素的数组,例如变量 defaultArray。

perhaps the problem is line 1 and 3也许问题是第 1 行和第 3 行

暂无
暂无

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

相关问题 为什么我不能在componentWillMount中将状态记录到控制台,而在componentDidMount中却可以? - Why can't I log a state to the console in componentWillMount but can in componentDidMount? 我可以使用控制台记录我的状态,但仍然会收到“无法读取未定义的属性&#39;data&#39;”。 为什么? - I can console log my state but I still get “Cannot read property 'data' of undefined”. Why? 为什么我不能访问事件处理程序中的状态和道具 - Why can't I access state and props inside event handler 当我输入内容时,onChange处理程序不会更新状态 - onChange handler isn't updating state when I type in input 为什么我的 onClick 处理程序没有注销最新版本的 state,我该如何解决这个问题? - Why does my onClick handler not log out the latest version of state and how can I troubleshoot this? 为什么当我尝试使用 redux 控制台 state 并反应 js 时,我的操作结果数据是 null - Why my action result data is null when I try console the state using redux and react js 如果我 console.log 在 axios 之外,则看不到我的 state 更新。然后 function - Can't see my state updated if i console.log outside the axios .then function 为什么当我尝试将数据推入其中时,我的 state 篮子一直是空的? [反应 JS] - why my state basket stay empty when i try to push data inside ? [REACT JS] 我找不到这个反应代码有什么问题,但是当尝试 console.log 该项目时,我对所有内容都未定义,除了注释字段 - I can't find what is wrong with this react code but when try to console.log the item, i get undefined for everything expect note field 为什么我无法更新我的反应组件的状态? - Why I can't get update the state of my react component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM