score:0

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.

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>
    );
}

score:3

you cant pass state upwards in react. it is one-directional. if you want the state in the parent you need to "lift the state up". 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


Related Query

More Query from same tag