[英]State is undefined when used in react router link inside useMemo hook
I am using react router to navigate to another page and pass state to that page using this inside useMemo我正在使用 react router 导航到另一个页面,并在 useMemo 中使用这个将状态传递到该页面
const columns = React.useMemo(
() => [
{
Header: "Actions",
id: "actions",
Cell: (tableProps) => {
return (
<>
<Link
to={{
pathname: "list-table/list-table-edit",
state: { selectedRow },
}}
>
<span
style={{
cursor: "pointer",
color: "grey",
textDecoration: "underline",
}}
onClick={(event) => {
setSelectedID(tableProps.row.original.asset_ID);
}}
>
<Tooltip title="Edit Row">
<EditButton aria-label="edit">
<CreateIcon fontSize="small" />
</EditButton>
</Tooltip>
</span>
</Link>
</>
);
},
},
...MakeTableColumns,
],
[selectedRow]
);
My state are declared like this我的状态是这样声明的
const [selectedID, setSelectedID] = useState();
const selectedRow = oriData.find((row) => row.asset_ID === selectedID);
when user click Edit
, it will set row ID to selectedID
and selectedRow
finds row values and pass to list-table-edit
page.当用户单击Edit
,它会将行 ID 设置为selectedID
并且selectedRow
查找行值并传递到list-table-edit
页面。 however, when I console.log props.location.state
from list-table-edit
, it's showing undefined
.但是,当我从list-table-edit
控制台props.location.state
时,它显示undefined
。 When I take out <Link> ..
outside of useMemo
and give a status id, it is working fine, I think it's something wrong with useMemo
.当我在useMemo
之外取出<Link> ..
并给出状态 ID 时,它工作正常,我认为useMemo
。 If anyone could help, I would be very much appreciated, thanks如果有人可以提供帮助,我将不胜感激,谢谢
Updated更新
Now I change from state: { selectedRow }
to state: { tableProps.row.original }
and pass to another page and it works without passing the state.现在我从state: { selectedRow }
更改为state: { tableProps.row.original }
并传递到另一个页面,它无需传递状态即可工作。 I could have already had the selected row value by calling tableProps.row.original
.我可以通过调用tableProps.row.original
获得选定的行值。
<Link to={{ pathname: '/list-table/list-table-edit', state: { record: selectedRow} }}>My route</Link>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.