[英]call a function in child component not set parent state in react js
I'm calling a function in my child component that is passed to child with props , and in this function I want to change parent's state but it not work;我正在调用我的子组件中的一个函数,该函数通过 props 传递给 child,在这个函数中,我想更改父组件的状态,但它不起作用; all of values passed true but setItems not work and I don't know what can I do .
所有值都通过了 true 但 setItems 不起作用,我不知道我能做什么。 it is my child component :
这是我的子组件:
export default function CartableList(props) {
function handleCallDetails(id, org) {
(props.handleSetItems)(items); // items that passed are true
}
.
.
.
return (
.
.
.
<Link
to={{
pathname: `/${window.location.pathname.split('/')[1]}/${props.orgID}/approval/cartable/details/${item.id}`,
search: `?current=${props.mode}`,
}}
onClick={() => handleCallDetails(item.id)}
className="drt_ItemLinkEl" />
.
.
.
);
.
.
.
}
There is my parent component :有我的父组件:
export default function Cartable(props) {
const [items, setItems] = useState(null);
function handleSetItems(val) {
// val is true but setItems not work
setItems(...val);
}
console.log('items : ' , items); // always show null and can not pass items to another component beacause it's null
You should be using props.items
in place of items
while calling the props. handleSetItems
在调用
props. handleSetItems
您应该使用props.items
代替items
props. handleSetItems
props. handleSetItems
function handleCallDetails(id, org) {
(props.handleSetItems)(items);
}
in this funtion you are not using any of the argument you get(id ,org).在这个函数中,你没有使用你得到的任何参数(id,org)。 and in here:
在这里:
onClick={() => handleCallDetails(item.id)}
your passing id but in component you expect to get array您传递的 id 但在组件中您希望获得数组
and why there is a onClick in a Link, when page changes state get back to initial state.以及为什么在链接中存在 onClick,当页面更改状态返回到初始状态时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.