Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When calling super() in custom view, make sure to pass up the same props that your component's constructor was passed

Here is my code which produces

Warning: LoaderView(...): When calling super() in LoaderView, make sure to pass up the same props that your component's constructor was passed.

import * as React from "react";
import {ActivityIndicator, Text, View} from 'react-native'

    export class LoaderView extends React.Component {

        constructor(props) {
            super(props)
        }

        props = {
            title: "",
            loading: false
        }


        render() {
            if (this.props.loading) {
                return <View {...this.props} style={{flex: 1, justifyContent: 'center'}}>
                    <ActivityIndicator
                        size={"large"}
                        animating={true}/>

                    <Text
                        style={{textAlign: 'center', fontSize: 20, fontWeight: '500'}}>
                        {this.props.title}
                    </Text>
                </View>
            } else {
                return <View {...this.props} style={{flex: 1, justifyContent: 'center'}}>
                    <Text
                        style={{textAlign: 'center', fontSize: 20, fontWeight: '500'}}>
                        {this.props.title}
                    </Text>
                </View>
            }
        }

    }

How to resolve this warning in above code?

like image 987
Rahul Rastogi Avatar asked Nov 23 '17 06:11

Rahul Rastogi


People also ask

What does super () do in react native?

In React Native when using super(props) you can immediately access the props via this. props in the constructor while with only super() you can't. In other methods like render however you can always utilize this.

Why is props passed to the super () function in React?

Explanation: If we want to use this in the constructor, we need to pass it to super. If we want to use this. props inside the constructor we need to pass it with the super() function. Otherwise, we don't want to pass props to super() because we see this.

What is the difference between super () and super props?

super() is used to call the parent constructor. super(props) would pass props to the parent constructor. From your example, super(props) would call the React. Component constructor passing in props as the argument.

Why we use constructor props and super props in React?

In React, the constructor is called during component creation and before mounting. If you want to implement the constructor for a React component, call the super(props) method before any other statement. Otherwise, this. props will be undefined in the constructor and create bugs.

Why do we have to call Super(props) in constructor?

super (props) is required to be called in constructor to access this.props. If it is not called in constructor, then this.props will be undefined in constructor. If you have been using react for a while, then you must be calling super (props) in constructor. Why do we have to do so? super () is basically calling the parent function.

Is it necessary to always pass Super to the constructor?

But this.props would still be undefined between the super call and the end of your constructor: It can be even more challenging to debug if this happens in some method that’s called from the constructor. And that’s why I recommend always passing down super (props), even though it isn’t strictly necessary:

When to call Super(props) in the constructor of a React component?

When implementing the constructor for a React.Component subclass, you should call super (props) before any other statement. Otherwise, this.props will be undefined in the constructor, which can lead to bugs. class TodoApp extends React.Component { constructor(){ super(); console.log(this.props); // this will be undefined } ... }

Is it possible to call Super () without the props argument?

But somehow, even if you call super () without the props argument, you’ll still be able to access this.props in the render and other methods. (If you don’t believe me, try it yourself!) How does that work? It turns out that React also assigns props on the instance right after calling your constructor:


1 Answers

use defaultProps instead of props

static defaultProps = {
    title: "",
    loading: false
}
like image 81
yangguang1029 Avatar answered Nov 15 '22 11:11

yangguang1029