Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good practice to create base components and then extend them in React?

I'm just learning React and I am writing components using ES7 syntax. My idea is to create a base component which will contain some methods that I want all derived components to have. For example, I want to implement valueLink without the mixin, for two-way binding in all my components. Here is my idea:

class MyComponent extends React.Component {

    bindTwoWay(name) {
        return {
            value: this.state[name],
            requestChange: (value) => {
                this.setState({[name]: value})
            }
        }
    };
}

 

class TextBox extends MyComponent {
    state = {
        val: ''
    };

    render() {
        return (<div>
            <input valueLink={this.bindTwoWay('val')}/>
            <div>You typed: {this.state.val}</div>
        </div>)
    }
}

And it works just fine. However, I wasn't able to find much information about this method. It's not about valueLink, that was just an example. The idea is to have some methods in a base component and later extend that component so that derived components have all those methods, like the usual OOP way. So I would just like to know if this is perfectly fine or there are some flaws that I am not aware of. Thanks.

like image 409
tuks Avatar asked Feb 09 '16 20:02

tuks


People also ask

Do you need to extend React component?

A class component must include the extends React.Component statement. This statement creates an inheritance to React.Component, and gives your component access to React.Component's functions. The component also requires a render() method, this method returns HTML.

What is the correct way of creating a component in React?

As the React docs tell us, “The simplest way to define a component is to write a JavaScript function”. This is a normal JavaScript function except that it returns a JSX React element. JSX, if you're not familiar, is just a special type of HTML that is adapted for JavaScript.

Which class should be extended to create a class component in React?

createClass we can add mixins to components using a mixins property which takes an Array of available mixins. These then extend the component class.


2 Answers

I would not recommend to create your own base component and drive from them rather use composition as suggested by react documentation also. enter image description here https://reactjs.org/docs/react-component.html#overview

like image 160
Vikas Gupta Avatar answered Oct 21 '22 12:10

Vikas Gupta


This is totally fine, it is just basic inheritance. The caveat of using inheritance to replace mixins is that there is no multiple inheritance, so you cannot give your TextBox features of multiple base classes as you would when you give it multiple mixins. To get around this you can use component composition. In your case composition would not work directly as you defined in the example but there is an workaround to this.

First you need to define a composing component

export default (ComposedComponent) => {
  class MyComponent extends React.Component {
    constructor(props, state) {
      super(props, state);
      this.state = {
        val: ''
      };
    }
    bindTwoWay(name) {
      return {
        value: this.state[name],
        requestChange: (value) => {
            this.setState({[name]: value})
        }
      };
    }

    render() {
      return (
        <ComposedComponent 
          {...this.props}
          {...this.state} 
          bindTwoWay={this.bindTwoWay.bind(this)}
        />
      }
    }
  }

  return MyComponent
}

And then you define your component where you need some common features

import compose from 'path-to-composer';

class TextBox extends React.Component {
  render() {
    return (
      <div>
        <input valueLink={this.props.bindTwoWay('val')}/>
        <div>You typed: {this.props.val}</div>
      </div>
    )
  }
}

export default compose(TextBox)

I have not tested this but it should work.

like image 40
Jarno Lonardi Avatar answered Oct 21 '22 13:10

Jarno Lonardi