Reactjs 未调用Redux选择器
我有以下情况: “我的应用程序状态”中国家列表的选择器Reactjs 未调用Redux选择器,reactjs,redux,reselect,Reactjs,Redux,Reselect,我有以下情况: “我的应用程序状态”中国家列表的选择器 export const getCountries = state => state.countries; 以及使用“重新选择”创建的选择器,用于筛选具有特定id的国家/地区 export const getCountryById = countryId => createSelector( [getCountries], (countries) => {
export const getCountries = state => state.countries;
以及使用“重新选择”创建的选择器,用于筛选具有特定id的国家/地区
export const getCountryById = countryId =>
createSelector(
[getCountries],
(countries) => {
let countriesList = countries.filter(c => c.id === countryId);
if ( countriesList ) {
return countriesList[0];
}
return null;
}
)
然后有一个国家/地区详细信息页面组件
mapStateToProps正确地接收到countryId,但没有调用选择器。我错过了什么
还是使用选择器的方法是错误的?我知道我可以在CountryPage中使用州的完整国家列表并在那里进行筛选,但这里的最佳做法是什么?这是因为您的getCountryById实际上不是选择器。它是一个返回选择器的函数
考虑到您正试图为每个组件使用唯一的ID并进行过滤,您可能需要使用来获得正确的记忆行为
尽管如此,您的选择器也可以简化。你不需要一个过滤器,你需要一个发现,这意味着它也不是真正需要记忆的东西。这可以简化为:
const getCountryById=createSelector
[getCountries,state,id=>id],
countries,id=>countries.findc=>c.id==id | | null
然后用作:
const mapStateToProps=状态,ownProps=>{
返回{
国家:getCountryByIdstate,ownProps.match.params.countryId
}
};
import React from 'react';
import { getCountryById } from '../redux/selectors';
import { connect } from 'react-redux';
const CountryPage = ({country}) => {
return <h1>{country.name}</h1>
}
const mapStateToProps = (state, ownProps) => {
console.log(ownProps.match.params.countryId);
const obj = {
country: getCountryById(ownProps.match.params.countryId)
}
return obj;
};
export default connect(mapStateToProps, null)(CountryPage);