Skip to content

Instantly share code, notes, and snippets.

@robcalcroft
Last active July 16, 2019 20:53
Show Gist options
  • Save robcalcroft/9c4fbe7538e0fd27bd77e45a209a0b6f to your computer and use it in GitHub Desktop.
Save robcalcroft/9c4fbe7538e0fd27bd77e45a209a0b6f to your computer and use it in GitHub Desktop.
Why render props mean messy code
function App(things) {
const newData = useGetNewData(things);
function handleClick() {
if (newData.length > 0)) {
// Woohoo
}
}
return (
<>
<h1>Hello</h1>
{newData.map(thing => (
<div>{thing} is really cool</div>
))}
<button type="button" onClick={handleClick}>Click me</button>
</>
)
}
function App() {
return (
<MyRenderProp data={things}>
{({ newData }) => (
<div>{newData.things}</div>
)}
</MyRenderProp>
);
}
function App() {
function handleClick() {
if (newData.length > 0)) {
// Hmm this is gonna fail as we don't have access to `newData`
}
}
return (
<MyRenderProp data={things}>
{({ newData }) => (
<h1>Hello</h1>
{newData.map(thing => (
<div>{thing} is really cool</div>
))}
<button type="button" onClick={handleClick}>Click me</button>
)}
</MyRenderProp>
);
}
function App() {
return (
<MyRenderProp data={things}>
{({ newData }) => (
<div>{newData.things}</div>
<FormHandlerRenderProp initialValue={newData.otherThing}>
{({ values }) => (
<input value={values.thing} />
)}
</FormHandlerRenderProp>
)}
</MyRenderProp>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment