私はまだネイティブに反応することが新しく、状態がtrueに設定されている場合にのみビューをレンダリングしようとしています。しかし、私はいつも次のエラーで終わる努力を続ける限り:未定義はオブジェクトではありません(_this.state)
Undefined is not an object (evaluating '_this.state')
コード:
"use strict"
import React, { Component } from 'react'
import * as Animatable from './animations'
import Icon from 'react-native-vector-icons/FontAwesome'
import renderIf from './libs/renderif'
import { Text, TouchableOpacity, View, StyleSheet, Navigator } from 'react-native'
var MyCustomComponent = Animatable.createAnimatableComponent(MyCustomComponent);
const Ic = ({ name, style, onPress }) => (
<TouchableOpacity onPress={onPress}>
<Icon name={name} style={style} />
</TouchableOpacity>
)
const Btn = ({label, onPress}) => (
<TouchableOpacity style={styles.btn} onPress={onPress}>
<Text style={styles.btnText}>
{label}
</Text>
</TouchableOpacity>
)
const Dashboard = ({ navigator }) => (
<View style={styles.container}>
<Text style={styles.text}>
In Dashboard
</Text>
<Btn label="Let's go back" onPress={() => navigator.pop()} />
</View>
)
const Home =() => ( // this is where I am trying to access the state
<View>
<View style={styles.navbarContainer}>
<View style={styles.navbar}>
<View style={styles.flexes}>
<Ic
name="bars"
style={styles.iconLeft}
onPress={() => this.setState({isOpen: true})}
/>
</View>
<View style={styles.flexes}>
<Text style={styles.navbarTitle}>
title
</Text>
</View>
<View style={styles.rightBtn}>
<Ic
name="envelope"
style={styles.iconRight}
onPress={() => alert("envelope pressed")}
/>
</View>
</View>
</View>
{renderIf(this.state.isOpen)(
<View style={{marginTop: 40}}>
<Text>hi</Text>
</View>
)}
</View>
)
export default class App extends Component {
constructor(props){
super(props)
this.state = {
isOpen: false
}
}
renderScene(route, navigator) {
return(
<route.component
navigator={navigator}
/>
)
}
render() {
return (
<Navigator
renderScene={this.renderScene.bind(this)}
initialRoute={{
component: Home
}}
/>
)
}
}
renderIf機能:
そして、メインインデックス。 android.js:
import { AppRegistry } from 'react-native'
import App from './app';
AppRegistry.registerComponent('NavRoute',() => App);
私は前に答えられた関連する質問を見つけようとしましたが、悲しいことに私が必要なものを見つけませんでした。この質問に以前に答えがあった場合は、そのスレッドと不便な点について深くお詫び申し上げます。
ありがとうございます!
、あなたはrenderIfを削除して、このようなものを使用しようとしました: '{(this.state.isOpen)の場合{リターン( ..... )}他{/ *他に何か* /}} ' –
Mark