博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
redux,react-redux、redux-thunk、redux-logger、redux-promise实例
阅读量:5096 次
发布时间:2019-06-13

本文共 6541 字,大约阅读时间需要 21 分钟。

使用的是create-react-app脚手架

package.json增加反向代理

"proxy": {    "/v4": {      "target": "https://m.maizuo.com",      "changeOrigin": true,      "pathRewrite": {        "^/v4": "/v4" }, "ws": true } }

 redux实例:

src/index.js

import React from 'react'import ReactDOM from 'react-dom'import App from './App'import {createStore, combineReducers} from 'redux';// step1: 创建reducersconst reducers = combineReducers({    films: function(state=[], action) {        let {type, payload} = action;        switch(type) {            case "GET_FILM_DATA":                var newS = [...state];                newS = payload;                return newS;            default:                return state;        }    }})// step:2 创建storeconst store = createStore(reducers, {});// 把store传递给组件function renderPage() {    ReactDOM.render(
, document.getElementById('root'));}renderPage();// step3: 注册回调store.subscribe(renderPage)

src/App.js

import React, { Component } from 'react';import axios from 'axios';class App extends Component {    componentDidMount() {            var that = this;            axios.get("/v4/api/film/now-playing?__t=1512647107236&page=1&count=5")            .then(function(res){                console.log(res);                that.props.store.dispatch({                    type: "GET_FILM_DATA",                    payload: res.data.data.films                })            })        }        render() {            var films = this.props.store.getState().films;            return (                
    { films.map((item, index)=>{ return
  • {item.name}

  • ; }) }
) } }export default App;

react-redux、redux-logger、redux-thunk实例

src/index.js

import React from 'react'import ReactDOM from 'react-dom'import App from './App'import {createStore, combineReducers, applyMiddleware} from 'redux';import logger from 'redux-logger';import ReduxThunk from 'redux-thunk';import {Provider} from 'react-redux';// step1: 创建reducersconst reducers = combineReducers({    films: function(state=[], action) {        let {type, payload} = action;        switch(type) {            case "GET_FILM_DATA":                var newS = [...state];                newS = payload;                return newS;            default:                return state;        }    }})// step:2 创建storeconst store = createStore(reducers, {}, applyMiddleware(ReduxThunk, logger));// 把store传递给组件ReactDOM.render(
, document.getElementById('root'));

src/App.js

import React, { Component } from 'react';import axios from 'axios';import {connect} from 'react-redux';//UI组件class AppUI extends Component {    componentDidMount() {        this.props.getFilmData();    }    render() {        return (            
    { this.props.films.map((item, index)=>{ return
  • {item.name}

  • ; }) }
) }}// mapStateToPropsconst mapStateToProps =(state)=> { return { films: state.films }}// mapDispatchToPropsconst mapDispatchToProps =(dispatch)=> { return { getFilmData: function() { //因为使用了thunk中间件,dispatch可传入回调函数 dispatch(function(dispatch){ axios.get("/v4/api/film/now-playing?__t=1512647107236&page=1&count=5") .then(function(res){ console.log(res); dispatch({ type: "GET_FILM_DATA", payload: res.data.data.films }) }) }); } }}// connectconst App = connect(mapStateToProps, mapDispatchToProps)(AppUI);export default App;
View Code

react-redux、redux-logger、redux-promise实例

src/index.js

import React from 'react'import ReactDOM from 'react-dom'import App from './App'import {createStore, combineReducers, applyMiddleware} from 'redux';import logger from 'redux-logger';import ReduxPromise from 'redux-promise';import {Provider} from 'react-redux';// step1: 创建reducersconst reducers = combineReducers({    films: function(state=[], action) {        let {type, payload} = action;        switch(type) {            case "GET_FILM_DATA":                console.log(payload);                var newS = [...state];                newS = payload;                return newS;            default:                return state;        }    }})// step:2 创建storeconst store = createStore(reducers, {}, applyMiddleware(ReduxPromise, logger));// 把store传递给组件ReactDOM.render(
, document.getElementById('root'));

src/App.js

import React, { Component } from 'react';import axios from 'axios';import {connect} from 'react-redux';//UI组件class AppUI extends Component {    componentDidMount() {        this.props.getFilmData();    }    render() {        return (            
    { this.props.films.map((item, index)=>{ return
  • {item.name}

  • ; }) }
) }}// mapStateToPropsconst mapStateToProps =(state)=> { return { films: state.films }}// mapDispatchToPropsconst mapDispatchToProps =(dispatch)=> { return { getFilmData: function() { var promise = new Promise(function(resolve, reject){ axios.get("/v4/api/film/now-playing?__t=1512647107236&page=1&count=5") .then(function(res){ resolve(res.data.data.films); }) }) dispatch({ //redux-promise中间件使得redux可以处理promise对象 type: "GET_FILM_DATA", payload: promise }) } }}// connectconst App = connect(mapStateToProps, mapDispatchToProps)(AppUI);export default App;
View Code

 react中怎么做到如vue中keep-alive的效果?

vue中keep-alive是将组将存入内存,实质上我们想要是保留state,因此可以用redux保存要 keep-alive的组件状态值即可

转载于:https://www.cnblogs.com/fanlinqiang/p/8001292.html

你可能感兴趣的文章
css3之transform-origin
查看>>
[转]JavaScript快速检测浏览器对CSS3特性的支持
查看>>
Master选举原理
查看>>
[ JAVA编程 ] double类型计算精度丢失问题及解决方法
查看>>
小别离
查看>>
微信小程序-发起 HTTPS 请求
查看>>
WPF动画设置1(转)
查看>>
backgound-attachment属性学习
查看>>
个人作业——关于K米的产品案例分析
查看>>
基于node/mongo的App Docker化测试环境搭建
查看>>
java web 中base64传输的坑
查看>>
java 中的线程(一)
查看>>
秒杀9种排序算法(JavaScript版)
查看>>
素数判断BFS之“Prime Path”
查看>>
Activiti入门 -- 环境搭建和核心API简介
查看>>
struts.convention.classes.reload配置为true,tomcat启动报错
查看>>
MySQL的并行复制多线程复制MTS(Multi-Threaded Slaves)
查看>>
Django中间件
查看>>
xcode 5.1安装vvdocument
查看>>
好玩的-记最近玩的几个经典ipad ios游戏
查看>>