无法使用Redux,Sagas访问第二个组件中的第一个组件状态

时间:2019-07-24 04:22:03

标签: reactjs react-redux redux-saga

第一成分

import React from "react";
import ReactDOM from "react-dom";
import PropTypes from 'prop-types'
import { withRouter } from "react-router-dom";
import { gateway as MoltinGateway } from "@moltin/sdk";
import {getList,updateList} from "./../Action/Action";
import { connect } from "react-redux";
import Icon from '@material-ui/core/Icon'; 
import Payment from "./../Payment/Payment";
import Tick from './done.png'
export class Item extends React.Component {
  constructor(props) {
    super(props);
    this.state = {};
    this.pickItem = this.pickItem.bind(this);
  }
  UpdateList ={}
  pickItem(pickedItem, id) {
    //console.log(pickedItem,id)
    document.getElementById(id).classList.toggle("active")
    this.UpdateList = pickedItem.map(function(data,i){
        if(data.id == id && i<=5 && data.pickedItem!=='Yes'){
          data.pickedItem = 'Yes'
          return data
        }else{
          data.pickedItem = 'No'
          return data
        }
    });
  }
  componentWillMount() {
    this.props.getList();
  }
  updateList(){
    //console.log(this.UpdateList)
    this.props.updateList(this.UpdateList)
    this.props.history.push({
      pathname: '/Payment',
    });
  }
  render() {
    //const { pickedItem } = this.state;
    const  {list} = this.props
    let filtereDate;
    if(list!==undefined && list.length>0){
      filtereDate = list.map(function(data,i){
        if(i<=5){
          return(
            <div key={data.id} ref={data.id} id={data.id} onClick={this.pickItem.bind(this, list, data.id )} className='item-list'>
              <span className="tickMark"><img src={Tick} /></span>
              <div className="logoWarapper">
                <img
                  src="https://rukminim1.flixcart.com/image/660/792/jmdrr0w0/shirt/q/q/r/xxl-tblwtshirtful-sh4-tripr-original-imaf9ajwb3mfbhmh.jpeg?q=50"
                  width="100"
                  height="100"
                  alt=""
                />
              </div>
              <div className="itemWarapper">
                <h3>{data.name}</h3>
                <p>
                  <span>&#8377;</span>
                  <span>{data.id}</span>
                </p>
              </div>
            </div>
          )
        }

      }.bind(this));

    }

    return (
      <div className="ItemPage">
        <header>
          <h1>Online shopping</h1>
          <h2>Visit | Pick | Pay</h2>
        </header>

        {filtereDate}
        <div className="btnWrp">
          <button onClick={this.updateList.bind(this)} className="button">Make Payment</button>
        </div>
      </div>
    );
  }
}

Item.propTypes = {
  list: PropTypes.object,
  getList: PropTypes.func,
  updateList:PropTypes.func
}
function mapStateToProps(state){
  const Items= state
  return {
    list : Items.list
  }
}

const mapDispatchToProps = dispatch => ({
  getList: () => dispatch(getList()),
  updateList: (list) =>
    dispatch(updateList(list))
})
export default withRouter(connect(
  mapStateToProps,
  mapDispatchToProps
)(Item));

Sages文件

import { put, takeLatest, all, call,select } from "redux-saga/effects";
function* fetchNews() {
  const json = yield fetch(
    "http://petstore.swagger.io/v2/pet/findByStatus?status=available"
  ).then(response => response.json());

  yield put({ type: "GET_LIST_SUCCESS", json: json });
}
function * updateNewList(data){
  ///console.log(data.payload)
  yield put({ type: "GET_LIST_SUCCESS", list: data.payload });
}
function * fetchupateList(){
  const signals = yield select(store => store)
  console.log(signals)
}
function* actionWatcher() {
  yield takeLatest("GET_LIST_REQUEST", fetchNews);
  yield takeLatest("GET_UPDATED_LIST_REQUEST", fetchupateList);
  yield takeLatest("UPDATE_LIST_REQUEST", updateNewList);
}
export default function* rootSaga() {
  yield all([actionWatcher()]);
}

**第二部分**

import React from "react";
import ReactDOM from "react-dom";
import PropTypes from 'prop-types'
import { withRouter } from "react-router-dom";
import { connect } from "react-redux";
import { makeStyles } from "@material-ui/core/styles";
import TextField from "@material-ui/core/TextField";
import Button from "@material-ui/core/Button";
import {getList,updateList,getUpdatedList} from "./../Action/Action";


export  class Payment extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      pickedItem: [1, 2]
    };
  }
  componentWillMount() {
    this.props.getUpdatedList();
  }
  render() {
    console.log(this.props)
    const { pickedItem } = this.state;
    //console.log(pickedItem);
    return (
      <div className="PaymentPage">
        <div className="pageWrapper">
          <form noValidate autoComplete="off">
            <h1>Payment Details</h1>
            <TextField
              id="outlined-name"
              label="Card Type"
              margin="normal"
              variant="outlined"
            />
            <TextField
              id="outlined-name"
              label="Card Name"
              margin="normal"
              variant="outlined"
            />
            <TextField
              id="outlined-name"
              label="Card Number"
              margin="normal"
              variant="outlined"
            />
            <div className="clm-2-inp">
              <TextField
                id="outlined-name"
                label="Expiry Date (MM/YYYY)"
                margin="normal"
                variant="outlined"
              />
              <TextField
                id="outlined-name"
                label="CVV"
                margin="normal"
                variant="outlined"
              />
            </div>
          </form>
          <div className="itemsection">
            <h2>Summery</h2>
            <div className="item-list">
              <div className="logoWarapper">
                <img
                  src="https://rukminim1.flixcart.com/image/660/792/jmdrr0w0/shirt/q/q/r/xxl-tblwtshirtful-sh4-tripr-original-imaf9ajwb3mfbhmh.jpeg?q=50"
                  width="100"
                  height="100"
                  alt=""
                />
              </div>
              <div className="itemWarapper">
                <h3>Item Name</h3>
                <p>
                  <span>&#8377;</span>
                  <span>3000</span>
                </p>
              </div>
            </div>

            <Button variant="contained" color="primary">
              Submit Purchase
            </Button>
          </div>
        </div>
      </div>
    );
  }
}


Payment.propTypes = {
  list: PropTypes.object,
  getList: PropTypes.func,
  updateList:PropTypes.func,
  getUpdatedList:PropTypes.func
}
function mapStateToProps(state,ownProps){
  console.log(state,ownProps)
  const Items= state
  return {
    list : Items.list
  }
}


const mapDispatchToProps = {
  getList,
  updateList,
  getUpdatedList
};
export default withRouter(connect(
  mapStateToProps,
  mapDispatchToProps
)(Payment));

减速器

const initialState = {
  list: {}
}
const Reducer = (state = initialState, action) => {
  switch (action.type) {
    case "GET_LIST_SUCCESS":
      return {
        ...state,
        list: action.json,
      }
      case "GET_LIST_SUCCESS":
      return {
        ...state,
        list: action.list,
      }
    default:
      return state;
  }
};

export default Reducer;

一旦我单击第一个组件中的“付款”按钮,我将通过一些修改来更新列表,这些修改是我想在第二个组件中获得的。 我无法在第二个组件中获得第一个redux存储值。

请帮助我解决这个问题。

0 个答案:

没有答案