简体   繁体   English

我没有在子组件中获得反应道具的预期价值

[英]I don't get the expected value of a react props in child component

I'm trying to manage some clients in a react js application (that I'm maintaining), it was first written in classes components but I'm now adding a functional child component and im not sure if this is the source of the problem or not (react-table examples only use functional component)我正在尝试在 React js 应用程序(我正在维护)中管理一些客户端,它首先是用类组件编写的,但我现在正在添加一个功能性子组件,我不确定这是否是问题的根源或不(反应表示例仅使用功能组件)

I have a main component that will do the data GET from a rest API and save it in state " entries " then I passe it to a child component as a props to render the data in a react-table, the problem is in this section as I have some buttons to edit and delete the data in react-modal, when I try access the props.entries after the buttons clicks I have an empty array of props.entries.我有一个主要组件,它将从 rest API 获取数据并将其保存在 state “条目”中,然后我将其作为道具传递给子组件以在反应表中呈现数据,问题出在本节中因为我有一些按钮可以在反应模式中编辑和删除数据,所以当我尝试在单击按钮后访问 props.entries 时,我有一个空的 props.entries 数组。

Here's the sandbox of the issue: https://codesandbox.io/s/stale-prop-one-forked-r6cevx?file=/src/App.js这是问题的沙箱: https://codesandbox.io/s/stale-prop-one-forked-r6cevx?file=/src/App.js

I did a console.log when the delete button is clicked, and you can see that en entries array is empty.单击删除按钮时,我做了一个 console.log,你可以看到 en entries 数组是空的。

You need to pass the showEditModal & showEditModal in useMemo dependency array.您需要在 useMemo 依赖项数组中传递showEditModalshowEditModal Since you dependency array is empty, when you click on edit or delete, it just points to the old function reference and it have the old entries value.因为你的依赖数组是空的,当你点击编辑或删除时,它只是指向旧的 function 引用并且它有旧的条目值。

You can check the entries values by console.log.您可以通过 console.log 检查条目值。

Hope this solve your problem希望这能解决你的问题

    const showEditModal = useCallback(
        (client_id) => {
            const tmpClient = props.entries.filter(function (el) {
                return el._id === client_id;
            })[0];
            setClient(tmpClient);
            setEditModal(true);

            console.log('aaa', props);

            console.log(client_id);
            console.log(props.entries);
            console.log(tmpClient);
        },
        [props.entries]
    );

    const showDeleteModal = useCallback(
        (client_id) => {
            console.log('showDeleteModal entries : ', entries);
            const tmpClient = entries.filter(function (el) {
                return el._id === client_id;
            })[0];
            setClient(tmpClient);
            setDeleteModal(true);

            console.log('Delete', entries);
            console.log(client_id);
            console.log(tmpClient);
        },
        [props.entries]
    );

    const columns = React.useMemo(
        () => [
            {
                Header: 'fact',
                accessor: 'fact'
            },
            {
                Header: 'Actions',
                accessor: 'length',
                Cell: ({ cell }) => (
                    <>
                        {cell.row.values.length}
                        <Tooltip title='Supprimer' placement='top'>
                            <IconButton
                                variant='outlined'
                                id={cell.row.values._id}
                                onClick={() => showDeleteModal(cell.row.values.length)}
                            >
                                <DeleteIcon />
                            </IconButton>
                        </Tooltip>
                        <Tooltip title='Modifier' placement='top'>
                            <IconButton
                                variant='outlined'
                                id={cell.row.values.length}
                                onClick={() => showEditModal(cell.row.values.length)}
                            >
                                <EditIcon />
                            </IconButton>
                        </Tooltip>
                    </>
                )
            }
        ],
        [showEditModal, showDeleteModal]
    );

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

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