![](/img/trans.png)
[英]Forwarding the checkbox state change to the parent component using callback React Hooks
[英]React nested child component callback function cannot read parent's state (using Hooks)
我正在动态创建一个按钮列(在一个可编辑的表格中)。 我的用例是在选定的行上显示此按钮。
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}
...
当我在 handleAddRow 中打印 currData 时,它显示 []。 问题:然而,当在渲染中打印时,currData 有一些价值。 Q:如何在Parent中设置回调函数的访问状态? 也欢迎任何其他建议。
注意:我使用的是 react-table 库,我所有的编码都使用钩子。
编辑:根据建议,我使用了部分解决的箭头函数。 以下是问题:-
单击事件的范围不限于您的组件。 要么绑定this
,要么使用箭头函数。
捆绑:
onClick={handleAddRow.bind(this)}
箭头函数:
onClick={() => handleAddRow()}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.