Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6214

React native context and deprecated super usage

$
0
0

I've been away from react native programming for a week and when I got back, after some VSCode updates, I have noticed that many of my super(props) calls in class constructors are now marked as deprecated. The reason seems to be some legacy context API issue, which is explained at this link: React Native Legacy Context

I have understood some of the issues affecting context usage from the link. However, I am now a bit confused as to whether I need to make a call to super(), super(props) or not to make the call at all. My previous understanding was that, writing a class that extends a base class, always requires a call to super(). If the base class constructor also uses any props received in the constructor, passing the props with super(props) is also required.

In my code, I almost always extend React.Component if I need a stateful component. I rarely need to use this.props in constructor()s, and if I do, I only use it to set up the initial state object, after which I deal with changes in lifecycle methods. The following is how most of my class components would look like:

class ExampleComponent extends React.Component {    constructor(props){        super(props);  // super marked as deprecated here        // super();    // super NOT marked as deprecated here        this.state = {            value: this.props.initialValue || 0        };    }    componentDidUpdate = (prevProps, prevState, snapshot) => {        // I would not actually do that, but for the sake of an example        if (this.state.value > 10){            this.setState({ value: 10 });        }    }    increment = () => {        const value = this.state.value + 1;        this.setState({ value });    }    render = () => {        return <View><Text>Current value is: { this.state.value }</Text><TouchableOpacity onPress={ this.increment }><Text>Add one!</Text></TouchableOpacity></View>;    }}

Can someone help me understand the correct usage of super in a React Native environment? I should also mention that I am using Expo SDK 38, which was released based on React 16.11. It is unclear to me whether the deprecation above also affects this version of React/React native or not.


Viewing all articles
Browse latest Browse all 6214

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>