简体   繁体   English

如何从 ReactJS 中的另一个组件传递 useState 挂钩的值?

[英]How to pass a value for useState hook from another component in ReactJS?

I have two components:我有两个组件:

Parent.js父.js

import { useState } from "react";

function Parent() {
    const [showHideContent, setShowHideContent] = useState("none");

    return (
        <div style={{ display: showHideContent }}>
            Some content here...
        </div>
    );
}

Child.js Child.js

function Child() {
    return (
        <button>
            Show/Hide Content
        </button>
    );
}

I want to pass two values none and block (one value at a time) through setShowHideContent of Parent component using Show/Hide Content button of Child component .我想使用子组件Show/Hide Content按钮通过父组件setShowHideContent传递两个值noneblock (一次一个值)

How to do this?这个怎么做?

NOTE: These two components are saved in the same folder but in two different files.注意:这两个组件保存在同一个文件夹中,但保存在两个不同的文件中。

These two component are rendered by App.js .这两个组件由App.js呈现。

<Route path="/content">
    <Menu /> {/* rendering in LEFT */}
    <div className="content-flexbox">
        <Parent /> {/* rendering in CENTER */}
        <Child /> {/* rendering in RIGHT */}
    </div>
    <Footer /> {/* rendering in BOTTOM */}
</Route>

It seems like you want the Child component to simply toggle the display value of some content in the Parent component.似乎您希望Child组件简单地切换Parent组件中某些内容的display值。

As you've defined them though they are not parent-child, but rather they are siblings.正如您定义的那样,尽管它们不是父子,但它们是兄弟姐妹。 As such if they need to share state/behavior, then the solution is to lift state up to a common ancestor, App in this case.因此,如果他们需要共享状态/行为,那么解决方案是将 state 提升到一个共同的祖先,在这种情况下是App

<Route path="/content">
  <Menu />
  <div className="content-flexbox">
    <Parent /> // <-- siblings
    <Child />  // <-- siblings
  </div>
  <Footer />
</Route>

See: Lifting State Up参见:向上吊起 State

Move the showHideContent state and updater into the parent App component, pass down the showHideContent state to Parent and the toggleVisibility callback to the Child .showHideContent state 和更新程序移动到父App组件中,将showHideContent state 传递给Parent并将toggleVisibility回调传递给Child

function Parent({ showHideContent }) {
  return <div style={{ display: showHideContent }}>Some content here...</div>;
}

function Child({ onClick }) {
  return (
    <button type="button" onClick={onClick}>
      Show/Hide Content
    </button>
  );
}

export default function App() {
  const [showHideContent, setShowHideContent] = React.useState("none");

  const toggleVisibility = () =>
    setShowHideContent((value) => (value === "none" ? "block" : "none"));

  return (
    ...
    <Route path="/content">
      <Menu />
      <div className="content-flexbox">
        <Parent showHideContent={showHideContent} />
        <Child onClick={toggleVisibility} />
      </div>
      <Footer />
    </Route>
    ...
  );
}

编辑 how-to-pass-a-value-for-usestate-hook-from-another-component-in-reactjs

You can simply pass values as prop to child component.您可以简单地将值作为道具传递给子组件。 Call your child component inside parent component and pass value在父组件中调用您的子组件并传递值

import { useState } from "react";
import Child from "//your path";

function Parent() {
const [showHideContent, setShowHideContent] = useState("none");

return (

    <div style={{ display: showHideContent }}>
       <Child showHide={showHideContent}/>
    </div>
);

} Now in your child component现在在你的子组件中

function Child({showHide}) {
return (
    <button>
        {showHide}
        Show/Hide Content
    </button>
);
}

Include your Child component in your Parent component and pass hooks as props将您的Child组件包含在您的Parent组件中,并将钩子作为道具传递

import { useState } from "react";
// import the child component
function Parent() {
    const [showHideContent, setShowHideContent] = useState("none");
    return (
        <div style={{ display: showHideContent }}>
            Some content here...
            <Child showHideContent={showHideContent}/>
        </div>
    );
}

And use them in your child components并在您的子组件中使用它们

function Child({showHideContent}) {
    return (
        <button>
{showHideContent}
            Show/Hide Content
        </button>
    );
}

Another way of doing the same is by using Context API:另一种方法是使用Context API:

https://reactjs.org/docs/hooks-reference.html#usecontext https://reactjs.org/docs/hooks-reference.html#usecontext

  1. You can pass an inline function as prop from parent to child like this.您可以像这样将内联 function 作为道具从父母传递给孩子。
function Parent(){
   const [showHideContent, setShowHideContent] = useState("none");
   return (
    <Child onButtonClicked={(toValue) => setShowHideContent(toValue)} />
   )
}


function Child({ onButtonClicked }) {
    return (
        <button onClick={onButtonClicked} >
            Show/Hide Content
        </button>
    );
}
  1. Pass down exact state modifier with same name传递具有相同名称的确切 state 修饰符
function Parent(){
   const [showHideContent, setShowHideContent] = useState("none");
   return (
    <Child setShowHideContent={setShowHideContent} />
   )
}

//or with prop speading
function Parent(){
   const [showHideContent, setShowHideContent] = useState("none");
   return (
    <Child {...{setShowHideContent}} />
   )
}



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

相关问题 如何将 useState 值传递给另一个组件比较 - How to pass useState value to another component compare 我如何将值从组件传递到ReactJS中的另一个组件 - How can i pass a value from a component to another component in ReactJS 如何将 useState 值从一个组件传递到另一个组件 - How can I pass useState value from one component to another component 如何在 React.js 中将 useState 值从一个组件传递到另一个组件 - How to pass useState Value from One Component to another one in React.js 如何将使用 typescript 定义的 useState 挂钩传递给另一个反应组件? - how can i pass a useState hook defined with typescript down to another react component? 如何将 useState 从一个组件传递到另一个组件? - How do I pass useState from one component to another? 如何使用 useState() 钩子和 history.push() 将 state 传递给另一个组件 - How to pass state to another component using useState() hook and history.push() 如何使用 reactjs 将 Like 值从一个组件传递到另一个组件? - How to pass the Like value from one component to another component using reactjs? 如何将 useState state 传递给另一个组件? - How to pass useState state to another component? 如何使用 useState React hook 更改子组件中父组件的值 - How to change a value from parent component in a child component with useState React hook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM