将真/假状态从子级传递给父级 - React
我想更改子组件中的真/假状态并将其传递给实际定义状态的父组件。目前,这只会导致错误。
家长:
const PostTemplate = ({ data }) => {
const [isSlider, setIsSlider] = useState(false);
return (
<Layout class="page">
<main>
<Views setIsSlider={isSlider} {...data} />
</main>
</Layout>
)}
</>
);
};
孩子:
const Views = (data) => {
return (
<div
key="views"
className="post__gallery views"
>
{data.views.edges.map(({ node: view }) => (
<divy
onClick={() => setIsSlider(true)}
>
<GatsbyImage
image={view.localFile.childImageSharp.gatsbyImageData}
/>
<div
className="post__caption"
dangerouslySetInnerHTML={{
__html: view.caption,
}}
/>
</div>
))}
</div>
);
};
现在,这输出:
setIsSlider is not a function.
(In 'setIsSlider(true)', 'setIsSlider' is false)
也许也与 React 的 console.log 相关:
Can't perform a React state update on an unmounted component.
This is a no-op, but it indicates a memory leak in your application.
To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
I would like to change a true/false state in a child component and pass it to a parent component, where the state is actually defined. Right now, this results only in an error.
Parent:
const PostTemplate = ({ data }) => {
const [isSlider, setIsSlider] = useState(false);
return (
<Layout class="page">
<main>
<Views setIsSlider={isSlider} {...data} />
</main>
</Layout>
)}
</>
);
};
Child:
const Views = (data) => {
return (
<div
key="views"
className="post__gallery views"
>
{data.views.edges.map(({ node: view }) => (
<divy
onClick={() => setIsSlider(true)}
>
<GatsbyImage
image={view.localFile.childImageSharp.gatsbyImageData}
/>
<div
className="post__caption"
dangerouslySetInnerHTML={{
__html: view.caption,
}}
/>
</div>
))}
</div>
);
};
Right now, this puts out:
setIsSlider is not a function.
(In 'setIsSlider(true)', 'setIsSlider' is false)
Perhaps also relevant the console.log from React:
Can't perform a React state update on an unmounted component.
This is a no-op, but it indicates a memory leak in your application.
To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将
isSlider
作为 prop 传递。您应该传递setIsSlider
。请注意,React 中的状态管理是一个复杂的主题,您可能想要对它的一般工作方式进行一些研究。直接传递状态设置回调是可行的,但它不能很好地扩展到复杂的应用程序。
You're passing
isSlider
as a prop. You should be passingsetIsSlider
.Note that state management in React is a complex topic and you probably want to do some research into how it is done in general. Directly passing a state setting callback works, but it doesn't scale well to complex applications.
您不必传递状态变量,而是必须传递状态函数,如下所示:
父级
子级
您必须使用
data.
访问setIsSlider
函数,如下所示:Instead of passing the state variable you have to pass the state function like this:
Parent
Child
You've to use
data.<function_passed_via_props>
to access thesetIsSlider
function, like this:修复方法:将
setIsSlider={isSlider}
更改为setIsSlider={setIsSlider}
。但是,如果您需要跨组件管理多个状态。我建议使用 Redux。这可以集中您的常用状态,您可以使用 Redux 在任何组件中访问或更新这些状态。
To Fix: Change
setIsSlider={isSlider}
intosetIsSlider={setIsSlider}
.However, in case you need to manage more than a few states across components. I would suggest using Redux. This can centralize your common-used states, you can access or update these states in any component using Redux.