简体   繁体   English

React 嵌套子组件回调函数无法读取父组件的状态(使用 Hooks)

[英]React nested child component callback function cannot read parent's state (using Hooks)

I am creating a button column dynamically (in an editable table).我正在动态创建一个按钮列(在一个可编辑的表格中)。 My usecase is to show this button on selected rows.我的用例是在选定的行上显示此按钮。

    Component:
    const [currData, setCurrData] = useState([]);
    const [tableCols, setTableCols] = useState([]);

    useEffect:
    ...
        cols.forEach((item, index) => {
          if (item.accessor === 'addB') {
              cols[index].Cell = ({ cell }) => (
                (cell.row.index === lastRow) ? (
                <button id={cell.row.index} value={cell.row.usky} onClick={handleAddRow}>
                  Add Row
                </button>) : ''
              )
            }
          }
        );
        setTableCols (cols);

    ...
    useTable(
        {
            columns={tableCols}
            data={currData}
    ...

When I print currData inside handleAddRow, it shows [].当我在 handleAddRow 中打印 currData 时,它显示 []。 Issue: However when printed in render, currData has some value.问题:然而,当在渲染中打印时,currData 有一些价值。 Q: How can I make the callback function access state in Parent? Q:如何在Parent中设置回调函数的访问状态? Any other suggestions are also welcome.也欢迎任何其他建议。

Note: I am using react-table libary and all my coding uses hooks.注意:我使用的是 react-table 库,我所有的编码都使用钩子。

EDIT: As per suggestion, I used arrow function that solves partly.编辑:根据建议,我使用了部分解决的箭头函数。 Here are issues: -以下是问题:-

  1. The callback arrow function cannot read the state variable when invoked first time upon button click.回调箭头函数在按钮单击时第一次调用时无法读取状态变量。 But is able to read 2nd time onwards.但是能够从第二次开始阅读。
  2. The callback reads the copy of state variable (which controls the render ie controlled) during time of render from parent to child.回调在从父级到子级的渲染期间读取状态变量的副本(控制渲染即受控)。 But does not read latest value in state variable.但不读取状态变量中的最新值。 However the state variable shows latest value which shows up in render.然而,状态变量显示在渲染中显示的最新值。

The click event is not scoped to your component.单击事件的范围不限于您的组件。 Either bind this , or use an arrow function.要么绑定this ,要么使用箭头函数。

Binding:捆绑:

onClick={handleAddRow.bind(this)}

Arrow function:箭头函数:

onClick={() => handleAddRow()}

暂无
暂无

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

相关问题 使用回调 React Hooks 将复选框 state 转发到父组件 - Forwarding the checkbox state change to the parent component using callback React Hooks 父 React 组件不会在使用反应钩子的子项中更改 state 时重新渲染; - Parent React component not re-rendering on change state in child using react hooks; 使用回调和挂钩(useState,useEffect)从父级中的 onClick 事件更改 React 子组件的 state - Change React child component's state from an onClick event in parent, with callbacks and hooks (useState, useEffect) 从React中父组件的回调函数中的子组件访问变量,并在父组件中呈现返回的值 - Access variables from a child component in parent component's callback function in React and render the returned value in parent React hooks - 通过函数从子级设置父级的状态 - React hooks - set state of Parent from child via function React Hooks TypeScript 继承父 state 和 function 类型在子 - React Hooks TypeScript inheriting parent state & function types in child react native - 使用反应挂钩通知子组件中 state 更新的父组件? - react native - Notify parent component of state update in child component with react hooks? 如何使用 React Hooks 将 state 值传递给回调 function - How to pass state value to a callback function using React Hooks React js从父组件更改子组件的state - React js change child component's state from parent component 如何使用 React 中子组件的 state 更新父组件? - How to update parent's component using state from child component in React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM