redux快速上手

前端之家收集整理的这篇文章主要介绍了redux快速上手前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

redux快速上手

用reflux存在问题

我们cmdb项目目前是用react reflux 进行构建的,使用起来非常方便

@H_403_6@
  • 使用Pluginreflux-state-mixin可以快速地通过
    store.setState()方法修改修改组件之间共享数据。

  • 使用Plugin reflux-promise 可以用action().then().catch()解决异步请求回调

  • 当然在使用过程中也发现了一些问题

    @H_403_6@
  • reflux允许多个入口修改store。

    @H_403_6@
  • action修改store

  • store中的函数直接修改

  • 直接setState

  • 直接对reflux store 中定义的对象进行修改,无法修改成功。必须进行深拷贝

  • 高阶组件无法避免重复render (react性能优化)

  • store定义混乱

    @H_403_6@
  • 列表数据跟子数据定义在一块

  • 数据容易重复定义(圈子权限跟用户角色权限)

  • 使用redux

    为什么使用redux

    @H_403_6@
  • 公司其他项目逐渐改用redux

  • redux 在react生态圈是使用最火的一个框架(之一)

  • 使用redux 正好解决了上述问题

  • redux的优点

    @H_403_6@
  • 单一store 数据好管理

  • 配合react-redux 可以将数据派发倒任何一个子组件

  • 修改store的唯一方法是dispatch 一个action

  • step1:定义初始数据

    开始定义一个component的初始数据会定义在getInitialState中

    1. getInitialState(){
    2. return {
    3. list:[
    4. {id:1,name:'数据1'},{id:2,name:'数据2'},{id:3,name:'数据3'},]
    5. }
    6. }

    使用redux定义初始数据需要定义在一个reducer中,通过一个函数返回需要得到的数据

    1. const initialState =[
    2. {id:1,];
    3. function reducer(state = initialState,action) {
    4. return state
    5. }

    step2:创建store

    通过redux的createStore方法创建唯一的store

    1. const store = createStore(
    2. reducer
    3. )

    step3: 安装react-redux

    所有容器组件都可以访问 Redux store,可以使用React Redux 组件 <Provider> 来让所有容器组件都可以访问 store,
    而不必显示地传递它。只需要在渲染根组件时使用即可。

    1. import React from 'react'
    2. import { render } from 'react-dom'
    3. import { Provider } from 'react-redux'
    4. import { createStore } from 'redux'
    5. import reducer from './reducer'
    6. import App from './components/App'
    7.  
    8. let store = createStore(reducer)
    9.  
    10. render(
    11. <Provider store={store}>
    12. <App />
    13. </Provider>,document.getElementById('root')
    14. )

    step4 component读取 state

    定义 mapStateToProps 这个函数来指定如何把当前 Redux store state 映射到展示组件的 props 中。

    1. import React from 'react'
    2. import { connect } from 'react-redux'
    3.  
    4. let App=React.createClass({
    5. render(){
    6. const {list}=this.props;
    7. return(
    8. <div>
    9. <ul>
    10. {list.map((item)=><Item key={item.id} item={item}/>)}
    11. </ul>
    12. </div>
    13. )
    14. }
    15. })
    16.  
    17. const mapStateToProps = (state) => {
    18. return {
    19. list: state
    20. }
    21. }
    22.  
    23. export default connect(
    24. mapStateToProps,)(App)

    step5 通过action修改redux中的数据

    1. export function deleteItem(id)=>{
    2. return {
    3. type:'DELETE_ITEM',payload:{
    4. id:id
    5. }
    6. };
    7. }
    1. const initialState =[
    2. {id:1,action) {
    3. switch (action.type) {
    4. case 'DELETE_ITEM':
    5. return state.filter(item=>
    6. item.id !== action.id
    7. )
    8. default:
    9. return state
    10. }
    11. }

    step6 分发 action

    除了读取 state,connect()还能分发 action。可以定义 mapDispatchToProps() 方法,
    接收 dispatch() 方法并返回期望注入到展示组件的 props 中的回调方法

    1. import React from 'react';
    2. import {deleteItem} from '../actions/index'
    3. import { connect } from 'react-redux'
    4.  
    5. let App=React.createClass({
    6. render(){
    7. const {list,deleteItem}=this.props;
    8. return(
    9. <div>
    10. <ul>
    11. {list.map((item)=><Item key={item.id} deleteItem={deleteItem} item={item}/>)}
    12. </ul>
    13. </div>
    14. )
    15. }
    16. })
    17.  
    18. const mapStateToProps = (state) => {
    19. return {
    20. list: state
    21. }
    22. }
    23.  
    24. const mapDispatchToProps = (dispatch) => {
    25. return {
    26. deleteItem: (id) => {
    27. dispatch(deleteItem(id))
    28. }
    29. }
    30. }
    31.  
    32. export default connect(
    33. mapStateToProps,mapDispatchToProps
    34. )(App)
    1. import React from 'react'
    2.  
    3. let Item=React.createClass({
    4. render(){
    5. const {item}=this.props;
    6. return(
    7. <li>
    8. {item.name}<button onClick={()=>this.props.deleteItem(item.id))}}>删除</button>
    9. </li>
    10. )
    11. }
    12. })
    13. export default Item

    参考

    redux 中文文档
    redux todo

    猜你在找的React相关文章