Javascript 将道具传递给选择器,以便根据该道具进行筛选

Javascript 将道具传递给选择器,以便根据该道具进行筛选,javascript,reactjs,redux,reselect,Javascript,Reactjs,Redux,Reselect,我需要将道具传递给选择器,以便我可以从选择器获取单击项目的内容。然而,我无法通过道具。我试过这种方法,但没有成功 const mapStateToProps = createStructuredSelector({ features: selectFeatures(), getFeatureToEditById: selectFeatureToEditById(), }); handleFeatureEdit = (event, feature) => { event.pre

我需要将道具传递给选择器,以便我可以从选择器获取单击项目的内容。然而,我无法通过道具。我试过这种方法,但没有成功

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);
    }
  );
const mapStateToProps=createStructuredSelector({
功能:选择功能(),
getFeatureToEditById:selectFeatureToEditById(),
});
handleFeatureEdit=(事件、特征)=>{
event.preventDefault();
日志(“特性句柄”,特性);
常量对话框=(
this.props.hideDialog(null)}
/>
);
this.props.showDialog(dialog);
};
选择器.js
const selectFeatureState=state=>state.get(“featureReducer”);
const-selectFeatureById=(\uUx,道具)=>{
log(“props”,u,props);#如果我在这里得到特性的id
#然后我可以根据下面的id进行筛选并显示
#FeatureEditDialog组件中的结果
};
const selectFeatureToEditById=()=>
创建选择器(
选择FeatureState,
选择FeatureById,
(功能,featureId)=>{
console.log(“features”、features、featureId);
}
);
下面是完整代码的要点


只需将状态和道具从
MapStateTrops
传递给选择器即可

如果将选择器直接用作mapStateToProps函数,它将收到与mapState相同的参数:
state
ownProps
(在连接的组件上设置的道具)

一个简单的例子:

// 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' />
//简单选择器
export const getSomethingFromState=(state,{id})=>state.stuff[id]
//重新选择选择器
export const getStuff=createSelector(
从州政府那里得到一些东西,
(stuff)=>stuff
)
//将其用作mapDispatchToProps
const mapDispatchToProps=getSomethingFromState
const MyContainer=connect(mapDispatchToProps)(MyComponent)
//并在渲染时将id设置为容器中自己的道具
然而,您正在做一些奇怪的事情,比如映射选择器以供以后重用。它不是那样工作的,至少它不打算那样使用

您可以使用选择器检索状态片段,并将其作为道具传递给
connect
ed组件。无论何时状态发生变化,选择器都将重新运行(由于重新选择,会有一些缓存)。如果组件实际从Redux检索的内容确实发生了更改,它将重新渲染

因此,您的
FeatureEditDialog
组件也应该是连接的,并且应该能够从Redux状态检索它所需要的任何东西,只需在其自己的
connect
调用中使用道具(哪个功能、哪个id等等)


this.props.showDialog(dialog)也是一种很大的代码气味。;)

请添加更多代码..用完整代码@SantoshRamKunjirThanks更新了我的问题以共享知识。我正在努力学习所有伟大的软件包,如重选、不变、传奇。我现在有了选择器的概念。如果你有时间,请你看看这个好吗?如果答案有效,则应将其标记为已回答。