36. React Redux的使用(1) - yiqunkeke/react-jianshu-shangguigu-zty GitHub Wiki

只使用redux

  • 创建store
    import {createStore} from 'redux'
    import reducer from './reducer'

    const store = createStore(reducer)

    export default store
  • 创建reducer
   const defaultState = {
      inputValue: '',
      list: []
   }
   export default (state = defaultState, action) => {
      return state
   }
  • index.js入口文件中
   import React from 'react'
   import ReactDOM from 'react-dom'
   import TodoList from './TodoList'
   
   ReactDOM.render(TodoList, document.getElementById('root'))
   
  • 组件中使用 store中数据
   import React, {Component} from 'react'
   import stroe from './store'

   class TodoList extends Component {
      constructor(props) {
         super(props)
         this.state = store.getState()
      }
      
      render() {
        reture (
         <input value={this.state.inputValue}/>
        )
      }
   }

   export default TodoList