2017-06-10 8 views
0

私はこの問題に関してstackoverflowのスレッドに相談し、このvideoをクリック可能なボタンの作成方法について見てきましたが、まだエラーが発生しています。ここでリアクションネイティブボタンonPress setState

は私が間違っていたとソリューションは何が何であるかを知っている可能性があり

class Button extends Component{ 
    render(){ 
     return(
      <View> 
       <TouchableOpacity 
        onPress{() => { this.props.onPress() }}> 
        <Text>LOGIN</Text> 
       </TouchableOpacity> 
      </View> 
     ) 
    } 
}; 

export default class FirstPage extends Component{ 
    constructor(props) { 
     super(props); 
     this.state = {clicked: false}; 
    } 

    onTap =() => { 
     this.setState({ 
      clicked: true 
     }); 
     if (this.state.clicked){ 
      return <SecondPage/> 
     } 
    } 

    render(){ 
     return(
      <View> 
       <Button onPress={() => { this.onTap() }}/> 
      </View> 
     ); 
    } 
} 

は、しかし、私はこのエラー

<View> 
    <TouchableOpacity 
      onPress{() => { this.props.onPress() }}> 
       ^
      <Text>LOGIN</Text> 
    </TouchableOpacity> 

を得る私のコードですか?

答えて

1
<TouchableOpacity 
     onPress={() => { this.props.onPress() }}> 

     <Text>LOGIN</Text> 
</TouchableOpacity> 

忘れ "="

続けます。

export default class FirstPage extends Component{ 
    constructor(props) { 
     super(props); 
     this.state = {clicked: false}; 
    } 

    onTap =() => { 
     alert("点击"); 
     this.setState({ 
      clicked: true 
     }); 

    } 

    render(){ 

     return(
      <View> 
       {this.state.clicked? 
        <SecondPage /> 
        : 
       <Button onPress={() =>this.onTap()}/> 
       } 
      </View> 
     ); 
    } 
} 
class SecondPage extends Component{ 
    render(){ 
    return(
     <View> 
     <Text>第二页</Text> 
     </View> 
    ); 
    } 
} 
class Button extends Component{ 
    render(){ 
     return(
      <View> 
       <TouchableOpacity 
        onPress={() => { this.props.onPress() }}> 
        <Text>LOGIN</Text> 
       </TouchableOpacity> 
      </View> 
     ) 
    } 
}; 

AppRegistry.registerComponent('RNDemo',() => FirstPage); 
+0

OH!私はそれを逃したとは信じられません。しかし、私はそれを修正した後、このエラーが発生しました。警告:React.createElement:型が無効です - 組み込みコンポーネント用の文字列 またはクラス/関数(複合コンポーネント用)ですが、got:未定義です。 あなたが定義したファイルからコンポーネントをエクスポートすることを忘れた可能性があります。LoginForm.jsでコードを確認してください:33(返信時は) – iamacat

+0

@iamacatはデモを見ます。 –

+0

ありがとうございました! – iamacat

関連する問題