2017-11-23 5 views
0

を渡されたのと同じ小道具を渡すようにしてください。ここカスタムビューにスーパー()を呼び出すとき、コンポーネントのコンストラクタは

警告生成私のコードです:LoaderView(...):スーパーを呼び出します()LoaderViewで、あなたのコンポーネントのコンストラクタが 渡されたのと同じ小道具を渡すこと を確認してください。上記のコードでは、この警告を解決する方法

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> 
      } 
     } 

    } 

+1

は= 静的defaultPropsを試みる{ タイトル: ""、 ローディング:偽 }。 の代わりにprops = {...} –

答えて

3

使用defaultProps代わりの小道具

static defaultProps = { 
    title: "", 
    loading: false 
} 
+0

defaultPropsが動作する特定の理由はありますか、小道具ではありませんか? –

+0

@RahulRastogiコンストラクタに渡された小道具がクラスで定義されている小道具と異なる場合、それは暗黙のうちに "this.props"です。 – yangguang1029

関連する問題