Skip to content

Instantly share code, notes, and snippets.

@brentsowers1
Last active January 21, 2021 22:57
Show Gist options
  • Save brentsowers1/f981e3edc31d270666679f9b26e20e3b to your computer and use it in GitHub Desktop.
Save brentsowers1/f981e3edc31d270666679f9b26e20e3b to your computer and use it in GitHub Desktop.
React prop drilling
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