简体   繁体   English

Redux的mapStateToProps不在AngularJS和React之间映射道具

[英]Redux's mapStateToProps not mapping props between AngularJS and React

Our project is mainly written in AngularJS 1.5, but we are transitioning over to ReactJS. 我们的项目主要是用AngularJS 1.5编写的,但是我们正在过渡到ReactJS。 We're using react2angular to allow our AngularJS project to consume React components. 我们使用react2angular来允许我们的AngularJS项目使用React组件。 Our project is also using ngRedux to store certain data. 我们的项目还使用ngRedux来存储某些数据。 While I'm able to save certain data using Angular, I am unable to access the Redux store data via the mapped props. 虽然我可以使用Angular保存某些数据,但无法通过映射的道具访问Redux存储数据。

this.props.interval in CardController.js is undefined. this.props.interval中的CardController.js未定义。 I am able to get store data using this.props.store.getState() , but React is not able to detect if there has been a change in the Redux store in componentWillReceiveProps , so I don't think that's an option. 我可以使用this.props.store.getState()获取存储数据,但是React无法检测componentWillReceiveProps Redux存储是否发生了变化,因此我认为这不是一个选择。

CardContainer.js CardContainer.js

import { connect } from 'react-redux';
import CardController from './CardController';

const mapStateToProps = (state) => {
  return {
    interval: state.interval
  };
};

const mapDispatchToProps = null;

const ScorecardContainer = connect(
  mapStateToProps,
  mapDispatchToProps,
)(CardController);

export default CardContainer;

CardController.js CardController.js

import React from 'react';
import PropTypes from 'prop-types';
import Card from './Card';

export default class CardController extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      selected: this.props.interval,
      state: this.props.$scope.filterParams,
    };
  }

  render() {
    return (
      <div className="CardController">
        <Card
          selected={this.state.selected}
        />
      </div>
    );
  }
}

CardController.propTypes = {
  interval: PropTypes.string,
};

reducers.js reducers.js

import * as actionTypes from './actionTypes';

const initialState = {
  interval : 'week'
};

export const setInterval = (state, action) => {
  return {
    ...state,
    interval : action.interval
  };
};

export const reducer = (state = initialState, action) => {
  switch (action.type) {
    case actionTypes.SET_INTERVAL :
      return setInterval(state, action);
    default :
      return state;
  }
};

export default reducer;

Turns out that the problem was that I forgot to drill down. 原来问题是我忘了钻取。 Instead of state.interval , it should have been state.card.interval . 代替state.interval ,应该是state.card.interval

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM