[英]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
传递两个值none和block (一次一个值) 。
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>
...
);
}
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
function Parent(){
const [showHideContent, setShowHideContent] = useState("none");
return (
<Child onButtonClicked={(toValue) => setShowHideContent(toValue)} />
)
}
function Child({ onButtonClicked }) {
return (
<button onClick={onButtonClicked} >
Show/Hide Content
</button>
);
}
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.