Answer the question
In order to leave comments, you need to log in
What to do with common methods of React components?
I know that it is not recommended to inherit components in React, but what about common methods that will be used in several components. For example, we have a method:
getDefaultState() {
return {
sellCurrency: 'EUR',
buyCurrency: 'GBP',
}
}
export default class BaseComponent extends Component {
getDefaultState() {
return {
sellCurrency: 'EUR',
buyCurrency: 'GBP',
}
}
}
class Component1 extends BaseCoponent {
constructor(props) {
super(props);
this.state = this.getDefaultState();
}
}
class Component2 extends BaseComponent {
constructor(props) {
super(props);
this.state = this.getDefaultState();
}
}
Answer the question
In order to leave comments, you need to log in
TL;DR: Higher Order Components
https://facebook.github.io/react/docs/higher-order...
https://medium.com/@franleplant/react-higher-order...
https:// gist.github.com/sebmarkbage/ef0bf1f338a718...
https://egghead.io/lessons/react-react-fundamental...
https://www.sitepoint.com/react-higher-order-compo...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question