2017-07-02 318 views
1

我正在用Redux和React制作一个非常简单的ToDo列表。尝试添加待办事项时,出现“Uncaught ReferenceError:type is not defined”。我尝试了几件事,这里是我现在的代码。任何想法我做错了什么?谢谢!在React/Redux中获取“Uncaught ReferenceError:type is not defined”错误?

这里是我的容器:

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 
    ) 
} 

当尝试添加待办事项,我得到以下错误:

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'; 
+0

更换括号我现在添加了types.js文件,只有有ADD_TODO。 – userden

+0

你有没有试过用我的答案解决你的问题? –

回答

2

我认为主要的原因的错误是由于addTodo函数中的拼写错误造成的。用大括号

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

修复了这个错误,谢谢!我会在5分钟内接受你的回答,当时系统允许我。我仍然没有看到我的待办事项,我不知道我在做什么错了。 – userden

+1

@Suomi你可以创建另一个问题,我会尽力回答它。 –

+0

Thanks @ D-reaper,我在这里提出了一个问题https://stackoverflow.com/questions/44872537/cant-get-todo-items-to-show-up-redux-react – userden

相关问题