useSelector 钩子函数?

时间:2021-04-21 15:06:09

标签: reactjs redux react-hooks

我不能使用动作和状态元素,因为我的组件有自己的道具。 所以我正在使用 useSelector (react hook) 并且想对动作做同样的事情,但不知道如何去做......知道吗?

interface MyComponentProps {
  item: Item;
  theFunctionIWouldLikeToUse: Function;
}

const MyComponent= ({
  item, theFunctionIWouldLikeToUse
}: MyComponentProps ) => {

  const myStateItem: any= useSelector(
    (state: State) => state.myStateItem,
  ) ; //I would like to do the same for the function
};

我可以使用 useSelector 或 useDispatch 来获取当前处于连接状态的函数吗?

export default connect(() => ({}), {
  theFunctionIWouldLikeToUse,
})(withTranslation('admin')(MyComponent));

1 个答案:

答案 0 :(得分:2)

如果你想要分派动作,你可以使用 useDispatch

const dispatch = useDispatch();

...
dispatch(theFunctionIWouldLikeToUse());
// or dispatch({type: "action_name"});
...