
Security News
Software Engineering Daily Podcast: Feross on AI, Open Source, and Supply Chain Risk
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.
render-props
Advanced tools
A hrlper for component builder to simplify using Render Props or Component Injection
TL;DR
defaultProps.$ npm i --save render-props
import renderProps from 'render-props';
class MyComponent extends Component {
state = {};
componentDidMount() {
this.timer = setInterval(() => {
const currentCount = this.state.count || 0;
this.setState({ count: currentCount + 1 });
}, 5000);
}
componentWillUnmount() {
clearInterval(this.timer);
}
render() {
const { render } = this.props;
return renderProps(render, this.state);
}
}
You can use any of the following and they will all render properly.
const RenderCountSFC = ({ count, foo }) => (
`Count = ${count} foo=${foo}`
);
RenderCountSFC.defaultProps = {
foo: 'foo',
count: 0,
};
class RenderCount extends Component {
render() {
const { count, foo } = this.props;
return (
`Count = ${count} foo=${foo}`
);
}
}
RenderCount.defaultProps = {
foo: 'foo',
count: 0,
};
const App = () => (
<div>
<h2>Traditional Render Prop</h2>
<MyComponent
render={
({ count, foo }) => (`Count = ${count} foo=${foo}`)
}
/>
<h2>Component Injection (SFC)</h2>
<MyComponent render={RenderCountSFC} />
<h2>Using Component Injection (class)</h2>
<MyComponent render={RenderCount} />
</div>
);
This will work no matter what you pass in the render prop.
You can pass a function, a Stateless Functional Component (SFC), or a class component.
In any case, if will be called to do the rendering.
Plus, if you pass a SFC, if will be rendered by calling it directly.
This is a huge performance boost over using JSX/React.createElement.
This helper will also merge in any defaultProps that your component might be using.
FAQs
Easily and reliably support Render Props, Component Injection, and Function as a Child
The npm package render-props receives a total of 32 weekly downloads. As such, render-props popularity was classified as not popular.
We found that render-props demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.

Security News
GitHub has revoked npm classic tokens for publishing; maintainers must migrate, but OpenJS warns OIDC trusted publishing still has risky gaps for critical projects.

Security News
Rust’s crates.io team is advancing an RFC to add a Security tab that surfaces RustSec vulnerability and unsoundness advisories directly on crate pages.