将真/假状态从子级传递给父级 - React

发布于 2025-01-11 16:32:20 字数 1442 浏览 0 评论 0原文

我想更改子组件中的真/假状态并将其传递给实际定义状态的父组件。目前,这只会导致错误。

家长:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

往事随风而去 2025-01-18 16:32:20

您将 isSlider 作为 prop 传递。您应该传递setIsSlider

请注意,React 中的状态管理是一个复杂的主题,您可能想要对它的一般工作方式进行一些研究。直接传递状态设置回调是可行的,但它不能很好地扩展到复杂的应用程序。

You're passing isSlider as a prop. You should be passing setIsSlider.

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.

若水微香 2025-01-18 16:32:20

您不必传递状态变量,而是必须传递状态函数,如下所示:

父级

const PostTemplate = ({ data }) => {
  const [isSlider, setIsSlider] = useState(false);

  return (
        <Layout class="page">
          <main>
                <Views setIsSlider={setIsSlider} {...data} />
          </main>
        </Layout>
      )}
    </>
  );
};

子级

您必须使用 data. 访问 setIsSlider 函数,如下所示:

const Views = (data) => {
  return (
      <div
        key="views"
        className="post__gallery views"
      >
        {data.views.edges.map(({ node: view }) => (
          <divy
            onClick={() => data.setIsSlider(true)}
          >
            <GatsbyImage
              image={view.localFile.childImageSharp.gatsbyImageData}
            />
            <div
              className="post__caption"
              dangerouslySetInnerHTML={{
                __html: view.caption,
              }}
            />
          </div>
        ))}
      </div>
  );
};

Instead of passing the state variable you have to pass the state function like this:

Parent

const PostTemplate = ({ data }) => {
  const [isSlider, setIsSlider] = useState(false);

  return (
        <Layout class="page">
          <main>
                <Views setIsSlider={setIsSlider} {...data} />
          </main>
        </Layout>
      )}
    </>
  );
};

Child

You've to use data.<function_passed_via_props> to access the setIsSlider function, like this:

const Views = (data) => {
  return (
      <div
        key="views"
        className="post__gallery views"
      >
        {data.views.edges.map(({ node: view }) => (
          <divy
            onClick={() => data.setIsSlider(true)}
          >
            <GatsbyImage
              image={view.localFile.childImageSharp.gatsbyImageData}
            />
            <div
              className="post__caption"
              dangerouslySetInnerHTML={{
                __html: view.caption,
              }}
            />
          </div>
        ))}
      </div>
  );
};
情深如许 2025-01-18 16:32:20

修复方法:将 setIsSlider={isSlider} 更改为 setIsSlider={setIsSlider}

但是,如果您需要跨组件管理多个状态。我建议使用 Redux。这可以集中您的常用状态,您可以使用 Redux 在任何组件中访问或更新这些状态。

To Fix: Change setIsSlider={isSlider} into setIsSlider={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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文