Last active
January 21, 2021 22:57
-
-
Save brentsowers1/f981e3edc31d270666679f9b26e20e3b to your computer and use it in GitHub Desktop.
React prop drilling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from 'react'; | |
const ReactPropDrilling = () => { | |
const [counter, setCounter] = useState(0); | |
return ( | |
<div> | |
Main app container: | |
<CounterDisplay counter={counter} /> | |
<div> | |
{/* eslint-disable-next-line jsx-a11y/control-has-associated-label,react/button-has-type */} | |
Increment - | |
<button onClick={() => setCounter(oldValue => oldValue + 1)} /> | |
</div> | |
</div> | |
); | |
}; | |
const CounterDisplay = ({ counter }) => { | |
return ( | |
<div> | |
Actually I am quite complicated and have several levels deep before I show | |
the counter value | |
<SecondCounterDisplay counter={counter} /> | |
</div> | |
); | |
}; | |
const SecondCounterDisplay = ({ counter }) => { | |
return ( | |
<div> | |
Nope, not quite yet, one more level... | |
<ThirdCounterDisplay counter={counter} /> | |
</div> | |
); | |
}; | |
const ThirdCounterDisplay = ({ counter }) => { | |
return <div>OK, now I can display the counter value = {counter}</div>; | |
}; | |
export default ReactPropDrilling; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment