Reputation: 2035
In react native:
How to create a component that extends another component rather than extending basic component from react
So instead of:
export default class XXX extends Component {
I need to create a Class Base
export default class XXX extends Base {
Where
export default class Base extends Component {
the whole idea is to create a Base
component to use it as base class for all other components .
Upvotes: 1
Views: 3422
Reputation: 1561
Yes you can, just you have to create a base component like below :-
import React, {Component} from 'react';
export default class BaseReact extends React.Component{
constructor(props){
super(props)
}
navigate=(name)=>{
const {navigate} = this.props.navigation;
navigate(name)
}
}
Import that class inside your .js file:-
import BaseReact from '/*/*/*/*/*/*/screen/BaseReact';
export default class Splash extends BaseReact{
constructor(props){
super(props)
}
}
May be help to you to achieve your goal.
Thank you.
Upvotes: 2
Reputation: 2022
Yes, you can do that, just as described in your question.
However, the React community favors composition over inheritance: https://facebook.github.io/react/docs/composition-vs-inheritance.html
Upvotes: 0
Reputation: 3262
You can do something like that, but it has some limitation. For example you can't override the parent methods. You can use props
, send therm to parent component and call them in it. You can also use Higher-Order Components, add your logic to it and add render method for example to each extended component.
Upvotes: 0