Redux实操

一、实现在首页传入数据

1.src项目文件夹下面,有home、types、reducers、action文件夹,home下有一个Home.js,需要在这个js文件开头引入

import { connect } from 'react-redux'
import { getPatientsList } from "../action/studentAction";

第二个引入,5.中作介绍

2.在Home.js末尾

const mapStateToProp = state => ({
  listPatients: state.patientsListStore.listPatients
});
const mapDispatchToProp = dispatch => ({
  getPatientsList: self => dispatch(getPatientsList(self))
});
export default connect(
  mapStateToProp,
  mapDispatchToProp
)(Home);

3.types下有一个types.js,需要对字段进行定义

export const STUDY_LIST = "students_list";

4.reducers下有一个studyReducer.js,注意下面的case后的字段必须与types.js定义的字段相同

import * as TYPES from "../types/types";

const initialState = {
  listStudents: []
};
export default function studentsList(state = initialState, action) {
  switch (action.type) {
    case TYPES.STUDY_LIST:
      return {
        ...state,
        listStudents: action.text
      }
    default:
      return state;
  }
}

5.action下有一个studentAction.js,这个文件需要引入到1.中的开头

        7.行中listStudents是queries文件中定义的

        studentsList是上面4.中第5行定义的函数名

import React, { Component } from "react";
import * as TYPES from "../types/types";
import { listStudents,getStudy } from "../graphql/queries";
import { graphqlOperation, API } from "aws-amplify";

export function getStudyList(self) {
  return async dispatch => {
    const newEvent = await API.graphql(graphqlOperation(listStudents));
    console.log(newEvent);
    if (newEvent) {
      dispatch(changePatientsListState(newEvent.data.listStudents.items));
    }
  };
}
function changePatientsListState(studentsStatus) {
  return {
    type: TYPES.PATIENTS_LIST,
    text: studentsList
  };
}

二、首页点击查看详情,进入详情页。详情页下有多个子页,以其中一个子页SupportRecords.js为例。

src项目文件夹下面,有detail、types、reducers、action文件夹,detail下有一个SupportRecords.js,Detail.js。

1.Detail.js引入,第三个引入在下方中做介绍

import {connect} from 'react-redux';
import SupportRecords from "./SupportRecords"
import {getStudyStatus} from '../action/studyAction'

2.紧接一中的项目,一中的按钮

<Button
                        content="View Detail"
                        icon="right arrow"
                        labelPosition="right"
                        size="mini"
                        onClick={() => this.gotoDetailPage(students.userId)}
                      />

按钮跳转事件

  gotoDetailPage(userId) {
    window.location.href = "/Detail?userId=" + userId;
  }

3.在Detail.js中

async componentDidMount() {
        const {getStudyStatus} = this.props;
        const id = this.props.location.search.split('=')[1];
        getStudyStatus(id);
}

4.其余和一中相似,SupportRecords.js中开头引入

import { getStudentsList } from "../action/studyAction";
import { connect } from 'react-redux'

结尾引入

const mapStateToProp = state => ({
    listStudents: state.studentsListStore.listStudents,
    studyStatus: state.studentsListStore.listStudents
});
const mapDispatchToProp = dispatch => ({
    getStudentsList: self => dispatch(getStudentsList(self))
});
export default connect(
    mapStateToProp,
    mapDispatchToProp
)(SupportRecords);

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值