Maxiss
Maxiss

Reputation: 1056

useSelector hook for functions?

I can't use the actions and state elements, as my component has its own props. So i am using useSelector (react hook) and would like to do the same for actions, but can't figure out how to do it... Any idea?

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
};

Can I use useSelector or useDispatch to get my function, which is currently in connect?

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

Upvotes: 0

Views: 249

Answers (1)

Viet
Viet

Reputation: 12787

If you want dispatch actions, you can use useDispatch

const dispatch = useDispatch();

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

Upvotes: 2

Related Questions