简体   繁体   English

调用子组件中的函数未在反应 js 中设置父状态

[英]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.

相关问题 反应js设置状态从父组件到子组件 - react js set state from parent to child component React - 如果父状态发生变化,则调用子组件中的函数 - React - call function in child component if parent state changes 在React.JS中从子级调用父组件函数 - Call parent component function from child in React.JS 如何通过异步函数设置反应父组件状态,然后将此状态作为props传递给子组件? - how to set react parent component state by a async function and then pass this state to child as props? 调用父函数设置父状态后,React Child组件将挂载然后卸载 - React Child component will mount then unmount after calling parent function to set parent state 从父组件 React JS 调用 map function 时如何更新子组件的 state - How to update state of child component when it get called in map function from parent component React JS 在 React 中,父组件如何从响应中设置状态以及在子组件中执行的异步获取功能? - In React, how can a parent component set state from the response an async fetch function performed in a child component? React js从父组件更改子组件的state - React js change child component's state from parent component 反应:在子组件函数中未定义父组件的“ this.state” - React: 'this.state' of parent component is undefined inside a child component function React - 如何使用子组件设置我的父 state,使用 function 结构(不是类) - React - How can I set my parent state using child component, using function structures (not classes)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM