How do i get the state value which I have stored in the Child component to the Parent component in React without button OnClick events?
I have a child component which i call from the Parent and within the child component, i set state and get a specific value which is then stored within a state value. Now, how do i get the value from there and read it i开发者_开发知识库n the Parent component without using an onClick event ?
Here's my code snippet:
PARENT COMPONENT
import Child from "../Child"
const Parent = () => {
...
...
return(
...
...
<Child />
);
};
export default Parent
CHILD COMPONENT
function Child (props) {
...
...
const [query, setQuery] = useState("sample")
...
...
return(
...
...
);
}
export default Child
As you can see above, I initialize a state variable and assign "sample" to "query" How can I get this "query" readable within the Parent Component ? I don't want to use an Onclick event, so please help me.
Define the useState in parent and pass it down to child
const Parent = () => {
const [query, setQuery] = useState("sample")
...
...
return(
...
...
<Child query={query} setQuery={setQuery}/>
);
child
function Child ({query,setQuery }) {
精彩评论