简体   繁体   English

如何在 React.js 功能组件中将子组件 state 传递给父组件 state?

[英]How to pass child component state to parent component state in React.js functional components?

I want to pass the Child component state to parent component state , I was done once in a class base component but in the functional component, it doesn't work anymore.我想将子组件state给父组件state ,我在 class 基本组件中完成了一次,但在功能组件中,它不再起作用。

Here is my model below:这是我的 model 下面:

在此处输入图像描述

And that is the components那就是组件

const Search = () => {
    return(
        <div>
            <h1>Search Anything</h1>
            <form>
                <InuptForm />
                <input type="submit">Search</input>
            </form>
        </div>
    );
}

const InuptForm = () => {
    const [search, setSearch] = React.useState('');

    const handleChange = (e) => {
        setSearch(e.target.value);
    }

    return(
        <div>
            <h1>Search Anything</h1>
            <form>
                <input type="text" name="search" value={search} onChange={handleChange} />
            </form>
        </div>
    );
}

Or is there any chance to use useReducer hook to overcome the issue?或者有没有机会使用useReducer钩子来解决这个问题?

I would really appreciate it if anyone can help.如果有人可以提供帮助,我将不胜感激。

Thanks谢谢

you cant pass state upwards in React.你不能在 React 中向上传递 state。 it is one-directional.它是单向的。 if you want the state in the parent you need to "lift the state up".如果您想在父级中使用 state,则需要“抬起 state”。 that means place it in the parent component这意味着将它放在父组件中

do something like this:做这样的事情:

const Search = ({ handleChange }) => {
    return(
        <div>
            <h1>Search Anything</h1>
            <form>

                <input type="submit" onChange={handleChange}>Search</input>
            </form>
        </div>
    );
}

const InuptForm = () => {
    const [search, setSearch] = React.useState('');

    const handleChange = (e) => {
        setSearch(e.target.value);
    }

    return(
        <div>
            <Search handleChange={handleChange} />
        </div>
    );
}

I'm rendering <Search /> inside <InputForm /> here and then passing down the handleChange prop我在<InputForm /> <Search /> ,然后传递handleChange道具

States cannot be passed to Parent components from child component, instead what you can do is create state in parent component and then pass it as prop to child, so on change function in child will update state in parent component.状态不能从子组件传递给父组件,而是您可以做的是在父组件中创建 state 然后将其作为道具传递给子组件,因此更改子组件中的 function 将更新父组件中的 Z9ED39E2EA931586B53A985A69EZEF

const Search = () => {
    const [search, setSearch] = React.useState('');

    const handleChange = (e) => {
        setSearch(e.target.value);
    }

    return(
        <div>
            <h1>Search Anything</h1>
            <form>
                <InuptForm value={search} handler={handleChange} />
                <input type="submit">Search</input>
            </form>
        </div>
    );
}

const InuptForm = ({value, handler}) => {

    return(
        <div>
            <h1>Search Anything</h1>
            <form>
                <input type="text" name="search" value={value} onChange={(e) => handler(e)} />
            </form>
        </div>
    );
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM