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.
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.
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.
createClass we can add mixins to components using a mixins property which takes an Array of available mixins. These then extend the component class.
I would not recommend to create your own base component and drive from them rather use composition as suggested by react documentation also. https://reactjs.org/docs/react-component.html#overview
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With