react-yt - an Youtube player component using the render prop patter
github.com/BTMPL/react-ytPosted Dec 27, 2017, 5:03 PM UTC
Posted Dec 27, 2017, 5:03 PM UTC
My first thought on reading the headline was, "Patter? What's a patter? Oh shit, a new JS term I need to learn."
Dangit; can't unsee now ;/ sorry, reddit does not allow editing titles ..
Just a minor thing but why an actual prop called render and not use the children prop? That's what I tend to go with and I was wondering about reasons to use a different prop.
There's been some discussion on that online lately (dig back through Kent C Dodds and Michael Jackson's Twitter accounts). The thought is that having a function as a child is pretty surprising to most people, whereas an explicit prop named render is easier for people to pick up on.
IMO, both are reasonable choices and confusion can probably be avoided with minimal documentation.
At least for my OSS projects, I use child-functions all the time and don't personally feel compelled to change. (I have a subjective preference for their appearance, particularly when nested.)
Understandable if you're making it consumable to third parties. Using children as functions can definitely be odd. I've found some interesting usages though where you have something like so but you could do the same with a named prop too.
import React from 'react';
...
render() {
return React.Children.map(this.props.children, child => {
if (child) {
return child(someValue);
}
return null;
});
}
Yep. In fact, check out the render function for React-Router's <Route> component, and also the diff for the PR that changed it from nested ternaries to if statements.
Can you ELI5 the render props pattern? Seems like you're passing in the entire render method of the component as a prop, right?
That's basically the pattern - it's also called "Function as a Child", where you would do the same, but with the children prop:
<Parent>
{(props) => (
<div>{props.title}</div>
)}
</Parent>
This alows the parent component to call the function and pass it any data that it provides, while the developer has freedom of shaping how the data is consumed.
You can learn more at
https://medium.com/merrickchristensen/function-as-child-components-5f3920a9ace9
The YouTube video having it's own baked in progress bar really confused me for a bit.
Ahah, yeah, could have chosen a better one :D
Hi there.
I've created this component as I needed a Youtube player that I could customize a lot - so render prop is the obvious approach here.
This is also my first component created using TypeScript so if you see room for improvement, let me know :)
I am curious about your choice of Render props. You could expose your API using a HOC, or with React Context, component injection, FaCC. What makes you so sure that RP is the obvious pattern?
I consider render prop a kind of "inline HoC" - this way I don't have to create a specific component to consume all the props I get, and can write the internal layout / logic without having to focus on creating a component. The example provided uses only a subset of the props, and expanding it is trivial.
Context would be an overkill for this kind of usage, and it is still considered "experimental" (there's a lot of talk in the context RFC)
I don't think this is a popular pattern? Can you provide me some examples?
FaCC is render prop, just that you're using
renderprop instead ofchildrenprop - there's some talk on this one over just using FaC, which u/acemarke pointed out in other comment.This reminds me of the Zen of Python's There should be one -- and preferably only one -- obvious way to do it.
Choosing between 5 reasonable options to do the same thing can be rough
Not all of them are equally as reasonable though. Context is just wrong - experimental, might change in the future, sort of difficult to work with and just not intended for that, component injection is too implicit for my liking, FaC is render prop, just a bit easier to read. For this application, render prop/FaC seems to be the way to go - you can see all the arguments right away and they're next to the component that issues them (instead of PropTypes somewhere in the component that consumes them), but you can still rename and pass them to a standalone component if you like. So you get all the advantages of higher order components with that extra bit of explicitness added.
Yet everyone keeps saying React is so easy to learn and so much better than everything else but in every single thread I see people arguing on what approach to take to solve very, very simple problems.