简体   繁体   English

将 state 传回给父母,并在 React 中将其他道具从父母传给孩子

[英]Pass state back to parent AND pass other props from parent to child in React

I know how to pass the state from child to parent.我知道如何将 state 从孩子传递给父母。 And I know how to pass props from parent to child.而且我知道如何将道具从父母传给孩子。

However, the obvious way for doing both at the same time doesn't seem to work as I expected.但是,同时进行这两种操作的明显方法似乎并没有像我预期的那样起作用。 Please see an example below.请看下面的例子。

import React, { useState } from "react";

const Child = (props, { other }) => {
  // This is what I'd like to achieve but it ignores the {other} prop and doesn't received it from parent

  // const Child = ({ other }) => { // Works, but the sending of props from child to parent stops working
  // const Child = (props) => { // Works too but obviously the 'other' prop is not passed anymore
  return (
    <>
      Child
      <button onClick={() => props.setValue("New stuff")}>Click!</button>
      <p>{other}</p>
    </>
  );
};

const Parent = () => {
  const [value, setValue] = useState("Default value");

  return (
    <>
      Parent <Child setValue={setValue} other={"Something else"} />
      <p>{value}</p>
    </>
  );
};

export default Parent;

I tried passing both as {props, other} , (props, other) , ({props, {other}}) but nothing worked.我尝试将两者都传递为{props, other} , (props, other) , ({props, {other}})但没有任何效果。

Here is a link to Codesandbox.这是 Codesandbox 的链接

Use const Child = ({other, ...props }) => {使用const Child = ({other, ...props }) => {

CodeSandbox: https://codesandbox.io/s/romantic-gagarin-3cgto代码沙盒: https://codesandbox.io/s/romantic-gagarin-3cgto

The props parameter in this case actually contains both of the setValue and other props.本例中的props参数实际上包含setValueother props。

The destructuring syntax is also helpful for legibility here.解构语法也有助于此处的易读性。

import React, { useState } from "react";

const Child = (props) => {
  // destructure both setValue and other out of props
  const {setValue, other} = props
  return (
    <>
      Child
      <button onClick={() => setValue("New stuff")}>Click!</button>
      <p>{other}</p>
    </>
  );
};

const Parent = () => {
  const [value, setValue] = useState("Default value");

  return (
    <>
      Parent <Child setValue={setValue} other={"Something else"} />
      <p>{value}</p>
    </>
  );
};

export default Parent;

You can access other from props :您可以从props访问other

import React, { useState } from "react";

const Child = (props) => {
  return (
    <>
      Child
      <button onClick={() => props.setValue("New stuff")}>Click!</button>
      <p>{props.other}</p>
    </>
  );
};

const Parent = () => {
  const [value, setValue] = useState("Default value");

  return (
    <>
      Parent <Child setValue={setValue} other={"Something else"} />
      <p>{value}</p>
    </>
  );
};

export default Parent;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应:将 2 个道具从子组件传回同一 function 中的父组件 - React: Pass 2 props back to the parent component in the same function from the child 我们可以将 setState 作为 props 从一个组件传递到另一个组件,并在 React 中从子组件更改父状态吗? - Can we pass setState as props from one component to other and change parent state from child component in React? 如何在 React.js 中将 state 作为道具从父母传递给孩子,然后从该孩子传递给它的孩子 - How to pass state from parent to child as props then from that child to it s child as props in React.js 从孩子到父母反应传递道具未定义 - react pass props from child to parent undefined React 将 props 从父包装器传递给子代 - React pass props from parent wrapper to child 将道具从孩子传递到父母的反应导航 - Pass props from child to parent react navigation 如何在反应中将道具从父母传递给孩子? - How to pass props from parent to child in react? 我们可以在 React 中将 props 从 Parent 传递给 Child 组件,并将 props 设置为子组件的 state 吗? - Can we pass props from Parent to Child component in React, and set the props as state of child component? 反应:将 Child1 state 值传递给 Parent 并从 parent 传回 Child2 - React: Passing Child1 state value to Parent and pass back to Child2 from parent 如何在 React 中将 state 从孩子传递给父母? - How to pass a state from a child to a parent in React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM