美文网首页
redux数据操作

redux数据操作

作者: Volon | 来源:发表于2019-06-20 17:38 被阅读0次

action,reducer,store与页面之间的数据传递

action代码:

import axios from"axios";
const url="http://localhost:3000/users";
export const GET_POSTS="GET_POSTS"
export function getPost(){      
        let request=axios.get(url)
                return{
                    type:GET_POSTS,
                    payload:request
                }

        }

reducer代码:
rootReducer组件



import { combineReducers } from 'redux'
import {GET_POSTS} from "../actions/index";
const defaultState = {
    posts:[]
}
 function posts (state = defaultState, action) {
    switch (action.type) {
        case GET_POSTS:
            return {
                posts:action.payload.data
            }
        default:
            return state;
    }
}
const rootReducer=combineReducers({
    posts:posts
});
export default rootReducer

store代码:

import { createStore,applyMiddleware } from 'redux'
import rootReducer from '../reducers'
import ReduxPromise from "redux-promise"
const creatStoreWidthMiddleware=applyMiddleware(ReduxPromise)(createStore);
export default function configureStore(initialState) {
    const store = creatStoreWidthMiddleware(rootReducer,initialState)
    return store 
}

dome代码:

import React from "react";
import {connect} from "react-redux";
import {getPost} from "../actions/index"
 class Dome5 extends React.Component{
    constructor(props){
        super(props);
    }
    componentWillMount(){
        this.props.getPost();
    }
    render(){   
        console.log(this.props.postList)    
        return(
            <div>
                {this.props.postList.map((post,index)=>{
                    return(
                    <div key={index}>
                    <span>姓名:{post.firstName}</span>
                    <span>年龄:{post.age}</span>
                    </div>
                    )
                })}
            </div>
        )
    }   
}
function mapStateToProps(store){
    return{
        postList:store.posts.posts
    }
}
export default connect(mapStateToProps,{getPost})(Dome5)

[图片上传失败...(image-cddf40-1561023510530)]

相关文章

  • redux数据操作

    action,reducer,store与页面之间的数据传递 action代码: reducer代码:rootRe...

  • redux-saga

    redux-saga 一个用于管理Redux应用异步操作的中间件,使副作用(数据获取、浏览器缓存获取)易于管理、运...

  • 浅析redux-persist

    之前在项目中遇到redux的数据持久化问题,当页面刷新时,redux中存储的状态会重置,导致之前的操作无效,这要是...

  • 上手redux中间件,有这篇文章就够了!

    用过react的同学都知道在redux的存在,redux就是一种前端用来存储数据的仓库,并对改仓库进行增删改查操作...

  • Redux

    一、什么是Redux Redux是一个数据层的框架,React可以借助Redux来实现数据的传递Redux等于Re...

  • Redux-saga

    Redux-saga 概述 redux-saga是一个用于管理redux应用异步操作的中间件,redux-saga...

  • 14.redux代码中使用19-05-15

    安装redux devtools工具 打开 操作完成 Redux下如图: 介绍redux使用流程 1.安装:yar...

  • 两张图看懂 React Redux 架构和数据流

    React 技术栈 React - 视图层Redux - 状态、数据层React-Redux - 连接Redux-...

  • React Native的数据流控制:Redux和Mobx对比

    React Native的数据流控制 待补充~~ Redux or Mobx Redux和Mobx对比 Redux...

  • 使用useReducer和Context替代redux

    使用useReducer和Context替代redux 步骤: 将数据集中在一个store对象 将所有操作集中在r...

网友评论

      本文标题:redux数据操作

      本文链接:https://www.haomeiwen.com/subject/zdwjqctx.html