未定义不是一个函数(评估' _reactNativeRouterFlux.Actions.chat')

时间:2017-08-31 16:14:09

标签: reactjs react-native

我写的是

import {
    Actions,
} from 'react-native-router-flux';

   <TouchableOpacity
        onPress={()=>{
        Actions.chat({
        name:{this.state.name},
        });
   }}>
    <Text style={styles.bottonText}>
   Next
   </Text>
  </TouchableOpacity>

但我一直在Actions.chat();收到错误。我还需要考虑什么?

1 个答案:

答案 0 :(得分:0)

以下是 Home.js

的代码
import React, {Component} from 'react';
import{
    View,
    Text,
    StyleSheet,
    TextInput,
    TouchableOpacity,
} from 'react-native';

import {
    Actions,
} from 'react-native-router-flux';

export default class Home extends Component{
    state={
        name:"",
    };
    constructor(props) {
        super(props);

    }
    render(){
        return(
            <View>
                <Text style={styles.title}>
                    Enter your name :
                </Text>
                <TextInput
                    style={styles.nameInput}
                    placeholder='Rafin Karki'
                    onChangeText={(text)=>{
                        this.setState({
                            name:text,
                        })
                    }}
                    value={this.state.name}
                />
                <TouchableOpacity
                    onPress={()=>{
                        Actions.chat({
                            name:{this.state.name},
                        });
                    }}>
                    <Text style={styles.bottonText}>
                        Next
                    </Text>
                </TouchableOpacity>
            </View>
        );
    }
}

const styles=StyleSheet.create({
    title:{
        marginTop: 20,
        marginLeft: 20,
        fontSize: 20,
    },
    nameInput:{
        padding:5,
        height:40,
        borderWidth: 2,
        borderColor: '#000000',
        margin:20
    },
    bottonText:{
        marginLeft: 20,
        fontSize:20,
    },
});

App.js

的代码
import React, {Component} from 'react';
import Home from './components/Home';
import Chat from './components/Chat';

import {
    Router,
    Scene,
} from 'react-native-router-flux';

import {
    Platform
} from 'react-native';


export default class App extends Component{
    render(){
        return(
            <Router>
                <Scene key='root' style={{ paddingTop:Platform.OS==='ios'?64:54}}>
                    <Scene key='home' component={Home} title='Home'/>
                    <Scene key='chat' component={Chat} title='Chat'/>
                </Scene>
            </Router>
        );
    }
}

Chat.js

import React,{Component} from 'react';
import{
    View,
    Text,
} from 'react-native';

export default class Chat extends Component{
    render(){
        return(
            <View>
                <Text>
                    Hello {this.props.name}
                </Text>
            </View>
        );
    }
}