Skip to content

Instantly share code, notes, and snippets.

@LucaColonnello
Created August 5, 2016 15:39
Show Gist options
  • Save LucaColonnello/8f52946193977fcd6727318c2e727513 to your computer and use it in GitHub Desktop.
Save LucaColonnello/8f52946193977fcd6727318c2e727513 to your computer and use it in GitHub Desktop.
withRender HoC
import React, { Component } from 'react';
import { compose } from 'redux';
import { connect } from 'react-redux';
// supposed to receive a class and return a new function (curryed) that
// if called add a render function to the received class,
// rendering the received component passing down all the props
import { withRender } from './utils';
// container class with event handler and action creator dispatch
class AContainer extends Component {
onBtnClick() {}
componentDidMount(){}
}
// factory of AContainer, composed by redux connect and withRender
export const AContainerWithRender = compose(
connect(() => {}, {}),
withRender(AContainer)
)
// default version using A as dumb component
export default AContainerWithRender(A);
// usage in another file with default render
import React from 'react';
import AContainer from './AContainer';
React.render(<AContainer />, ...);
// usage in another file with custom render
import React from 'react';
import { AContainerWithRender } from './AContainer';
const AContainer = AContainerWithRender((props) => (
<div>My custom renderer</div>
));
React.render(<AContainer />, ...);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment