获取"未捕获的ReferenceError:未定义类型" React / Redux中的错误?

时间:2017-07-02 15:00:14

标签: reactjs redux react-redux

我正在使用Redux和React创建一个非常简单的ToDo列表。我得到了一个"未捕获的ReferenceError:未定义类型"在尝试添加待办事项时。我尝试了几件事,这是我目前的代码。我有什么想法,我做错了什么?谢谢!

这是我的容器:

import React, {Component} from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { addTodo } from '../actions/index';

class Todos extends Component {
    constructor(props) {
        super(props);
        this.state = {text: ''};
    }

    addTodo(e) {
        e.preventDefault();
        this.props.addTodo(this.state.text);
        this.setState({
            text: ''
        });
    }

    updateValue(e) {
        this.setState({text: e.target.value})
    }

    render() {
        return (
            <div>
                <form onSubmit={(e) => this.addTodo(e)}>
                    <input
                        placeholder="Add Todo"
                        value={this.state.text}
                        onChange={(e) => {
                            this.updateValue(e)
                        }}
                    />
                    <button type="submit">Add Todo</button>
                </form>
                <ul>
                    { this.props.todo.map((item) => {
                        return <li key={item.id}>{ item.message }</li>
                    })}
                </ul>
            </div>
        );
    }
}

function mapStateToProps({ todo }) {
    return { todo }
}

function mapDispatchToProps(dispatch) {
    return bindActionCreators({addTodo}, dispatch);
}

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

这是减速器:

import { ADD_TODO } from '../actions/types';

export default function(state=[], action) {
    switch(action.type) {
        case ADD_TODO:
            return [ action.payload.message, ...state ]
    }
    return state;
}

和../reducers / index.js

import { combineReducers } from 'redux';
import TodosReducer from './reducer_addTodo';

const rootReducer = combineReducers({
    todo: TodosReducer
});

export default rootReducer;

行动:

import { ADD_TODO } from './types';
const uid = () => Math.random().toString(34).slice(2);

export function addTodo(message) {
    const action = {
        id: uid(),
        message: message
    };
    return(
        type: ADD_TODO,
        payload: action
    )
}

尝试添加Todo项目时,出现以下错误:

Uncaught ReferenceError: type is not defined
    at addTodo (bundle.js:21950)
    at Object.addTodo (bundle.js:21166)
    at Todos.addTodo (bundle.js:23541)
    at onSubmit (bundle.js:23562)
    at Object.ReactErrorUtils.invokeGuardedCallback (bundle.js:4532)
    at executeDispatch (bundle.js:4332)
    at Object.executeDispatchesInOrder (bundle.js:4355)
    at executeDispatchesAndRelease (bundle.js:3785)
    at executeDispatchesAndReleaseTopLevel (bundle.js:3796)
    at Array.forEach (<anonymous>)

编辑:

这是types.js文件:

export const ADD_TODO = 'ADD_TODO';

2 个答案:

答案 0 :(得分:2)

我认为错误的主要原因是你的addTodo函数中的拼写错误。用大括号替换括号

export function addTodo(message) {
    const action = {
        id: uid(),
        message: message
    };
    return {
        type: ADD_TODO,
        payload: action
    };
}

答案 1 :(得分:0)

您只需更改即可使其完美工作,只需更改:

return { 
    type: 'ADD_TODO', //this is string 
    payload: action
};