Higher order components will allow you to apply behaviors to multiple React components.
So the idea is to create a high order component, then use this hight order component to create multi same behaivor component.
So high order function is insdie function return another function, the same, high order component is inside component return another component.
let Mixin = InnerComponent => class extends React.Component { constructor(){ super(); this.state = {val: 0} } update(){ this.setState({val: this.state.val + 1}) } componentWillMount(){ console.log('will mount') } render(){ return} componentDidMount(){ console.log('mounted') }}
Inside the Mixin Component, we define the behavior "update" which will be shared to the mixined components.
Define two component:
const Button = (props) => const Label = (props) =>
Mixin those two component with Mixin:
let ButtonMixed = Mixin(Button)let LabelMixed = Mixin(Label)
Use it:
class App extends React.Component { render(){ return (); }}ReactDOM.render(, document.getElementById('app'));
-----
let Mixin = InnerComponent => class extends React.Component { constructor(){ super(); this.state = {val: 0} } update(){ this.setState({val: this.state.val + 1}) } componentWillMount(){ console.log('will mount') } render(){ return} componentDidMount(){ console.log('mounted') }}const Button = (props) => const Label = (props) => let ButtonMixed = Mixin(Button)let LabelMixed = Mixin(Label)class App extends React.Component { render(){ return ( ); }}ReactDOM.render(, document.getElementById('app'));