首页 > 解决方案 > 在没有任何 mapstatetoprops 的情况下连接 redux 并且仍然将东西作为道具

问题描述

我在 redux 中看到了这种奇怪的连接方式,我很难理解正在做什么以及如何做。这是连接代码

export default connect(({ cricketFantasy: { matchDetails } }) => {
  let innings = [];
  let matchInfo = null;
  let currentOver = -1;
  let currentPlayer1Id = null;

  if (matchDetails && Object.keys(matchDetails).length) {
    const {
      homeTeam,
      awayTeam,
      tournament,
      gameDateTime,
      matchDescription,
      venue,
      currentInning,
      officials,
      squad,
      toss,
      matchState
    } = matchDetails;
    if (homeTeam && homeTeam.innings && homeTeam.innings.length) {
      homeTeam.innings.forEach(inning => {
        innings.push({
          order: inning.order,
          battingTeamName: inning.battingTeam,
          isCurrentInning: inning.id === currentInning.id
        });
      });
    }
   // some more operations which i deleted as that is not major concern 
  return {
    innings,
    matchInfo,
    currentOver,
    currentPlayer1Id,
    currentPlayer2Id,
    tournamentId,
    squad: squadObj,
    matchState: matchStateStr,
    isFetchingMatchDetail: false,
    routes,
    detailsData: matchDetails
  };
})(withStyles(styles)(withLocale(CricketScore)));

我尝试在组件的渲染方法中进行控制台登录,我看到返回的任何内容都可以视为道具。但是,我担心的是来自 ({ cricketFantasy: { matchDetails } }) 这来自。我没有看到在此代码所在的 .js 文件中的任何位置使用 cricketFantasy 一词。我也没有看到任何 mapStateToProps 。

标签: javascriptreactjsreduxreact-redux

解决方案


{ cricketFantasy: { matchDetails } }是一个解构赋值

它依赖于state包含一个cricketFantasy属性,该属性的值是一个具有matchDetails属性的对象。

基本上它只是一种奇特的形式:

const matchDetails = state.cricketFantasy.matchDetails;

或者

const { matchDetails } = state.cricketFantasy;

const mapStateToProps = ({ cricketFantasy: { matchDetails } }) =>
  console.log({ matchDetails });

const state = {
  cricketFantasy: {
    matchDetails: "Hello"
  }
};

const mapStateToProps2 = (state) => {
  const matchDetails = state.cricketFantasy.matchDetails;
  console.log({ matchDetails });
};

mapStateToProps(state);
mapStateToProps2(state);


推荐阅读