TypeError:未定义不是函数(评估“ store.getState()”)

时间:2018-09-12 09:07:32

标签: react-native react-redux

所以我正在尝试构建一个简单的页面,要求输入并提供一个按钮。当按下按钮时,页面应提醒输入值。但不会从输入框中获取值。

因此,输入框的值应保存在redux商店中,并在收到警报时从商店中获取该值。

但是当我运行代码时,它抛出一个错误,提示undefined不是函数

这是我的代码:

import React, {Component} from 'react';
import {View, TextInput, TouchableOpacity, Text} from 'react-native';
import {connect} from 'react-redux';

class tes extends Component{
    constructor(props){
        super(props)
        state = {
            phoneNumber: "",
        }
    }

    login() {
        this.props.onLogin(this.state.phoneNumber)
        alert(this.props.reducer.phoneNumber)
    }
    render(){
        return(
            <View>
                <TextInput placeholder="phone number"
                    keyboardType="number-pad" onChangeText={phoneNumber => this.setState({ phoneNumber })}/>
                <TouchableOpacity onPress={this.login}>
                    <Text>login</Text>
                </TouchableOpacity>
            </View>
        )}
}

mapStateToProps = (state) => {
    return {
        number: state.reducer
    }
}

mapDispatchToProps = (dispatch) => {
    return {
        onLogin: (number) => {
            dispatch({
                type: "LOGIN",
                payload: number
            })
        }
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(tes);

在动作课上:

export default function setLoginNumber(number) {
    return{
        type: "LOGIN",
        payload: number
    };
}

这是我的减速器类:

 const reducer = (state = {
    phoneNumber: '',
},action) => {
    switch(action.type) {
        case "LOGIN":
            state = {
                phoneNumber: action.payload
            }
            break;
    }
    return state;
}

export default reducer;

以及我使用redux创建商店的商店类:

import { createStore , applyMiddleware } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import reducer from './Reducer';
import thunk from 'redux-thunk';

const store = () => {
    return createStore(reducer, composeWithDevTools(applyMiddleware(thunk)));
}

export default store

这是模拟器显示的错误

the error from emulator

有人可以帮助我解决此错误吗?预先感谢

0 个答案:

没有答案