A
A
Alexander Show2020-09-06 11:27:19
React
Alexander Show, 2020-09-06 11:27:19

How to pass props between child functional components in React?

Good afternoon, there is a parent component App and two child components Header and Main how to pass props from header to main????

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
moody_good, 2020-09-09
@AlexShow163

It can be like this:

export default Header = ({ setProp }) => {
    return (
        <button onClick={() => setProp('hello')}>Ok</button>
    )
}

export default Main = ({ prop }) => {
    return (
        <div>{prop}</div>
    )
}

export default App = () => {
    const [mainProp, setMainProp] = useState('');
    return (
        <>
            <Header setProp={setMainProp} />
            <Main prop={mainProp}/>
        </>
    )
}

K
Kirill Makarov, 2020-09-06
@kirbi1996

create it in App and then pass it to the children, otherwise useContext can be used. In general, if there was a code, it would be possible to suggest

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question