javascript - 在没有任何 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 。
解决方案
{ 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);
推荐阅读
- sql - 被引用表中没有与外键中的引用列列表匹配的主键或候选键
- javascript - 警告:找不到密钥模拟器的依赖包!警告:无法计算完整的依赖项列表
- excel - 启用宏后,Excel VBA 找不到项目或库
- angular - 在角度通用中未发现管道错误
- swift - 快速将 UInt8 数组的一段包装为字符串的正确方法是什么?
- excel-formula - 在日期范围内查找excel
- node.js - Nodejs 后端中的电子商务产品过滤器
- karma-runner - 5.2.3 版本的 Karma 漏洞问题
- rx-java - 在同一个调度程序上订阅时,有没有办法保留主题发出顺序?
- javascript - 如何在 mongodb 的单独网页上创建单独的集合?