我需要将道具传递给选择器,以便我可以从选择器中获取所单击项目的内容。但是我无法通过道具。我试过这种方式但没有成功
const mapStateToProps = createStructuredSelector({
features: selectFeatures(),
getFeatureToEditById: selectFeatureToEditById(),
});
handleFeatureEdit = (event, feature) => {
event.preventDefault();
console.log("feature handle", feature);
const dialog = (
<FeatureEditDialog
feature={feature}
featureToEdit={selectFeatureToEditById(feature)}
onClose={() => this.props.hideDialog(null)}
/>
);
this.props.showDialog(dialog);
};
selectors.js
const selectFeatureState = state => state.get("featureReducer");
const selectFeatureById = (_, props) => {
console.log("props", _, props); #if i get the id of feature here
# i could then filter based on that id from below selector and show
# the result in FeatureEditDialog component
};
const selectFeatureToEditById = () =>
createSelector(
selectFeatureState,
selectFeatureById,
(features, featureId) => {
console.log("features", features, featureId);
}
);
以下是完整代码的要点
https://gist.github.com/MilanRgm/80fe18e3f25993a27dfd0bbd0ede3c20
答案 0 :(得分:1)
只需将状态和道具从mapStateToProps
传递给您的选择器。
如果直接使用选择器作为mapStateToProps函数,它将接收mapState所做的相同参数:state
和ownProps
(在连接组件上设置props)。
一个简单的例子:
// simple selector
export const getSomethingFromState = (state, { id }) => state.stuff[id]
// reselect selector
export const getStuff = createSelector(
getSomethingFromState,
(stuff) => stuff
)
// use it as mapDispatchToProps
const mapDispatchToProps = getSomethingFromState
const MyContainer = connect(mapDispatchToProps)(MyComponent)
// and set id as an own prop in the container when rendering
<MyContainer id='foo' />
但是,您正在做一些奇怪的事情,例如映射选择器以便以后重用它。它不会那样工作,至少它不打算以这种方式使用。
使用选择器检索状态切片并将其作为道具传递给connect
ed组件。每当状态发生变化时,您的选择器将重新运行(通过重新选择进行一些缓存)。如果组件实际从Redux检索的内容确实发生了变化,它将重新呈现。
因此,您的FeatureEditDialog
组件也应该连接,并且应该能够从Redux状态中检索所需的任何内容,只需在其自己的{{中使用props(哪个功能,哪个ID等)。 1}}打电话。
connect
也是一个很大的代码味道。 ;)