Note: If there is no slotslot element in the child’s template, any content from the parent will be silently discarded. Fallback Content. If the parent does not inject any content into the child’s slot, the child will render any elements inside its slotslot tag, like so. These are render props in React and scoped slots in Vue. They work very similarly and provide a way to separate the behavior of a component from its presentation. Render props in React First, let’s look at how we would restructure our autocomplete component using render props in React.
TLDR; I show that Vue can use JSX to utilize the React pattern of a render prop. Source code here
In Vue, templates are typically how we compose/extend/mix/open source our components. This is contra the experience of React developers who write most of their compiled html in JSX.
Thanks to a similar architecture of using the virtualDOM + createElement API and babel-plugin-transform-vue-js, we can write our Vue components in almost the same way we write React! (Not that we should do this for all components, but it's fun to see design patterns and utilize them).
UPDATE: I'm using render props in https://github.com/educents/vue-autosuggest in the renderSuggestion prop, so go check it out!
For demonstration, I will use an example from Use a Render Prop! article by Michael Jackson.
First the SFC:
Here in our parent App.vue
, the Mouse
component will be our child component. Inside Mouse.js
we will call our renderProp
function callback inside the render method. I've mixed JSX inside the SFC's methods
section as you can't use jsx inside template
. Here's our Mouse
component:
Yes this is a Vue component, not React. Compared with the React version:
Some differences between the two:
this
(the Vue instance) instead of the React state, but utilize destructuring all the same to pass back x
and y
.onMouseMove
vs onMousemove
💣Vue Render Prop
So there you go, a fairly similar and transferable component design.
Get our latest post in your inbox every Tuesday by subscribing to the Vue.js Developers Newsletter .
This subscription also includes Vue.js Developers promotional emails. You can opt-out at any time. View our privacy policy .
This form is protected by reCAPTCHA. The Google privacy policy and terms of service apply.
In case you are wondering what's the equivalent pattern in Vue, it's called scoped slots (and if using JSX it works the same as React)
— Evan You (@youyuxi) September 25, 2017Vue creator Evan You on render props.
If you were to do 'render props' with templates, a similar design would be to use scoped slots and would look something like this:
Vue Scoped slots are powerful when using templates.
Some advantages to scoped slots are that:
Why would I want to use the render prop pattern or JSX? The nice thing about this is that using the render function + JSX is a closer-to-the-compiler alternative and allows fine-grain control over rendering. You won't be able to use custom directives like v-model or v-if. Render props themselves have a lot of benefits, as outlined in this episode of Full stack radio where Adam interviews Kent C. Dodds.
If you're a Vue user, does this type of component composing surprise you? If so, I recommend going and reading the official docs which actually explain JSX and render functions in greater detail.
I love that we can share design principles between frameworks. It gives me warm fuzzy feelings in a cruel, cold world that believes there is a war on frameworks. In 2018, try and find the common ground.
If you enjoyed reading this, follow me on twitter where my DM's are always open!
Further reading: