React 简易学习(六、数据存储)

1.打开github

    redux:存储项目数据

    react-redux:协助调用redux中的数据

    redux-thunk:协助action导出非对象

    immutable:数据就是一旦创建,就不能更改的数据,保证store里面的数据不被修改

    redux-immutable:讲子界面中的reducer数据统一到总reducer时需要转换使用此包

1.2 创建项目中的store

    在src文件夹下创建一个header模块;
    在header模块下创建store目录;
    在header/store目录下创建四个文件为:actionCreater.js actionTypes.js index.js reducer.js

    actionCreater.js 处理数据的操作并传递给reducer,请求ajax信息
    actionTypes.js 存储常量,作为action和reducer的传递参数
    index.js 导出当前模块的action和reducer给当前模块和总的store调用
    reducer.js 处理当前模块的数据操作

    actionCreater.js

        //引入actionTypes 
       import * as actionTypes from './actionTypes'; 
        //引入ajax包 
        import axios from 'axios'; 
        //因为对store数据使用了此类型,在传递数据时也需要将数据转为immutable类型 
        import { fromJS } from 'immutable'; 

        const chagneList = (list,totalPage)=>({ 
          type:actionTypes.LIST_CHANGE, 
          list:fromJS(list), 
          totalPage:fromJS(totalPage) 
        })
        //由于导入redux-thunk 所以可以导出方法 
        export const AjaxListAction = ()=>{ 
              return (dispatch)=>{ 
                axios.get('/api/headerList.json').then(res=>{ 
                  //处理获取的数据 
                      if(res.data.result){ 
                        let totalPage = Math.ceil(res.list.length/10); 
                        dispatch(chagneList(res.list,totalPage)); 
                     } 
                }).catch(()=>{ 
                  //捕获异常 
                 console.log(error); 
                }) 
          } 
    } 


    actionTypes.js

        export const LIST_CHANGE = "list change";


    reducer.js

        import * as actionTypes from './actionTypes';
        import { fromJS } 'immutable';
        //将state变为不可更改的对象
        const defaultState = fromJS({
          list:[],
          totalPage:1
        });

        export default (state=defaultState ,action)=>{
          switch(action.type){
            case actionTypes.LIST_CHANGE:
                state.merge({
                list:action.list,
                totalPage:action.totalPage
              });
              //或者
              //state.set('list',action.list).set('totalPage',action.totalPage)
              return state;
           default:
              return state;
          }
    }


    index.js

        import reducer from './reducer';
        import * as actionCreater from './actionCreater';
        import * as actionTypes from './actionTypes';

        export {
          reducer,actionTypes,actionCreater
        }

1.3 创建模块中的store

    首先,在src文件夹下创建store目录,并在目录下创建store.js 和 reducer.js

    index.js 存储数据文件
    reducer.js 汇总模块传来的数据,并传递给store

    index.js 

        import { createStore,compose,applyMiddleware } from 'redux';
        import thunk from 'redux-thunk';
        import reducer from './reducer';

        const composeEnhancers = window.__REDUX_DEVTOOLS_EWTENSION_COMPOSE__||compose;

        const store = createStore(reducer,compostEnhancers(applyMiddleware(thunk)));
        export default store;


    reducer.js 

        import { combineReducers } from 'redux-immutable';
        import { reducer as headerReducer } from '../Header/store'

        const reducer = combineReducers({
          header:headerReducer
        })
        export default reducer;

1.4 在项目中部署

    在项目的外层渲染,加上下方代码

        import React from 'react';
        import Header from './Header';
        //引入此包方便调用store里面的数据
        import { Provider } from 'react-redux';
        //调用store里面的数据
        import store from './store';

        function App(){
          return (
            <div className="app">
             <Provider store={store}>
                <Header/>
              <Provider>
            <div>
          );
    }
    export default App;

1.5 在模块中使用

    在模块渲染文件中调用如下

        import React,{ Component } from 'react';
        import { connect } from 'react-redux';
        import { actionCreater } from './store';

        class Header extends Component{
          render(){
            let {list,totalPage} = this.props;
            return(
              <div></div>
            )}
            }
        //此方法讲store里面的数据转存到props中
        const mapStateToProps = (state)=>{
          return {
            totalPage:state.getIn(['header','totalPage']);
            list:state.getIn(['header','list']);
         }
        }
        const mapDispatchToProps = (dispatch)={
          return {
            //此方法中存储多个方法
            demo(){
              dispatch(actionCreater.AjaxListAction());
           },
          }
    }

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值