2017-06-14 84 views
0

我为我的申请创建了actionreducers。我正在尝试创建一个新的待办事项,并希望使用redux将其保存在状态中。如何使用REDX在待办事项列表中添加待办事项

动作/ index.js

let taskID = 0; 

export const addTodo = text => { 
    return { type: "ADD_TODO", text, id: taskID++ }; 
}; 

减速器/ todos.js

const todo = (state = {}, action) => { 
    switch (action.type) { 
    case "ADD_TODO": 
     return { 
     id: action.id, 
     text: action.text, 
     status: false 
     }; 
    default: 
     return state; 
    } 
}; 

export default todo; 

减速器/ index.js

import { combineReducers } from "redux"; 
import todos from "./todos"; 

const todoApp = combineReducers({ todo }); 

export default todoApp; 

index.js

import React from "react"; 
import ReactDOM from "react-dom"; 
import App from "./App"; 
import registerServiceWorker from "./registerServiceWorker"; 
import "./index.css"; 

import { Provider } from "react-redux"; 
import { createStore } from "redux"; 
import todoApp from "./reducers/todos"; 

let store = createStore(todoApp); 

ReactDOM.render(
    <Provider store={store}><App /></Provider>, 
    document.getElementById("root") 
); 
registerServiceWorker(); 

App.js

import React, { Component } from "react"; 
import logo from "./logo.svg"; 
import "./App.css"; 

import AppBar from "material-ui/AppBar"; 
import FloatingActionButton from "material-ui/FloatingActionButton"; 
import MuiThemeProvider from "material-ui/styles/MuiThemeProvider"; 
import * as strings from "./Strings"; 
import * as colors from "./Colors"; 
import styles from "./Styles"; 
import ContentAdd from "material-ui/svg-icons/content/add"; 
import Dialog from "material-ui/Dialog"; 
import FlatButton from "material-ui/FlatButton"; 
import * as injectTapEventPlugin from "react-tap-event-plugin"; 
import TextField from "material-ui/TextField"; 
import { List, ListItem } from "material-ui/List"; 
import { connect } from "react"; 
import { addTodo } from "./actions/index"; 

const AppBarTest =() => 
    <AppBar 
    title={strings.app_name} 
    iconClassNameRight="muidocs-icon-navigation-expand-more" 
    style={{ backgroundColor: colors.blue_color }} 
    />; 

class App extends Component { 
    constructor(props) { 
    injectTapEventPlugin(); 
    super(props); 
    this.state = { 
     open: false, 
     todos: [], 
     notetext: "" 
    }; 
    this.handleChange = this.handleChange.bind(this); 
    } 

    handleOpen =() => { 
    this.setState({ open: true }); 
    }; 

    handleClose =() => { 
    this.setState({ open: false }); 
    }; 

    handleCreateNote =() => { 
    let todos = [...this.state.todos]; 
    todos.push({ 
     id: todos.length, 
     text: this.state.notetext, 
     completed: false 
    }); 
    this.setState({ todos: todos },() => { 
     // setState is async, so this is callback 
    }); 
    this.handleClose(); 
    }; 

    handleChange(event) { 
    this.setState({ [event.target.name]: event.target.value }); 
    } 

    _renderTodos() { 
    return this.state.todos.map(event => { 
     return (
     <ListItem 
      primaryText={event.text} 
      key={event.id} 
      style={{ width: "100%", textAlign: "center" }} 
      onTouchTap={this._handleListItemClick.bind(this, event)} 
     /> 
    ); 
    }); 
    } 

    _handleListItemClick(item) { 
    console.log(item); 
    } 

    render() { 
    return (
     <MuiThemeProvider> 
     <div> 
      <AppBarTest /> 
      <FloatingActionButton 
      style={styles.fab} 
      backgroundColor={colors.blue_color} 
      onTouchTap={this.handleOpen} 
      > 
      <ContentAdd /> 
      </FloatingActionButton> 
      <Dialog 
      open={this.state.open} 
      onRequestClose={this.handleClose} 
      title={strings.dialog_create_note_title} 
      > 
      <TextField 
       name="notetext" 
       hintText="Note" 
       style={{ width: "48%", float: "left", height: 48 }} 
       defaultValue={this.state.noteVal} 
       onChange={this.handleChange} 
       onKeyPress={ev => { 
       if (ev.key === "Enter") { 
        this.handleCreateNote(); 
        ev.preventDefault(); 
       } 
       }} 
      /> 

      <div 
       style={{ 
       width: "4%", 
       height: "1", 
       float: "left", 
       visibility: "hidden" 
       }} 
      /> 

      <FlatButton 
       label={strings.create_note} 
       style={{ width: "48%", height: 48, float: "left" }} 
       onTouchTap={this.handleCreateNote} 
      /> 
      </Dialog> 

      <List style={{ margin: 8 }}> 
      {this._renderTodos()} 
      </List> 

     </div> 
     </MuiThemeProvider> 
    ); 
    } 
} 

export default App; 

我想保存里面handleCreateNote功能新的待办事项,我不知道如何使用商店,这里分派到保存的状态。谁能帮我 ?

+0

我可以推荐https://learnredux.com/,它可以处理使用redux创建应用程序的几乎所有内容。 – EatYaFood

回答

1

变化

export default App;

function mapStateToProps(state) { 
    return { 
     todo: todo 
    } 
} 
export default connect(mapStateToProps, actions)(App) 

你也应该使用

import * as actions from './action/index'; 

毕竟这些导入的所有动作修改这个功能如下: -

handleCreateNote =() => { 
    let todos = [...this.state.todos]; 
    let newTodo = { 
     id: todos.length, 
     text: this.state.notetext, 
     completed: false 
    }; 
    todos.push(newTodo); 
    this.setState({ todos: todos },() => { 
     // setState is async, so this is callback 
    }); 
    this.props.addTodo(this.state.notetext); 
    this.handleClose(); 
    }; 

您的添加待办事项的逻辑也不正确。 所以,你的行动创造者应该是这样的

let taskID = 0; 

export const addTodo = text => { 
    return { 
    type: "ADD_TODO", 
    text: text, 
    id: taskId++ 
    }; 
}; 

现在也减速器需要改变,那么这应该是这样的: -

const todo = (state = [], action) => { 
    switch (action.type) { 
    case "ADD_TODO": 
     let newTodo = { 
     id: action.id, 
     text: action.text, 
     status: false 
     }; 
     return [...state, newTodo] 
    default: 
     return state; 
    } 
}; 

export default todo; 

我希望这helps.Not最好的实现,但将解决您的问题。

+0

我想检查'handleCreateNote'里面的redux状态值是什么方式? –

+0

你可以做this.props.todo来检查状态。 – VivekN

+0

如果我想检查完整的商店,那么有什么办法吗?正如我有'让store = createStore(todoApp);'在'index.js'中,并且我想在另一个js文件中访问它以进行调试 –