A
A
Alexey Kondakov2018-02-20 10:07:28
JavaScript
Alexey Kondakov, 2018-02-20 10:07:28

How to rewrite action in a component?

Good afternoon!
There is a connection of the form

@connect(null, dispatch => ({
  handleFilters: (type, text) => dispatch(handleFilters(type, text))
}))

How to transfer the dispatch from it to the inside of actions?
export const handleFilters = (type, val) => {
  switch (type) {
    case "input":
      return {
        type: FILTER_BY_INPUT,
        payload: val
      };
    case "time":
      return {
        type: FILTER_BY_TIME,
        payload: val
      };
    case "hall":
      return {
        type: FILTER_BY_HALL,
        payload: val
      };
    case "status":
      return {
        type: FILTER_BY_STATUS,
        payload: val
      };
    case "dateFrom":
      return {
        type: FILTER_BY_DATE_FROM,
        payload: val
      };
    case "dateTo":
      return {
        type: FILTER_BY_DATE_TO,
        payload: val
      };
  }
};

I broke my whole head - I did not understand.
Important condition: bindActioncreators cannot be used.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2018-02-20
@Columnistdc

Would you like to understand why this transfer?
However, if you want to use dispatch inside your actions, you can use the redux-thunk middleware , then you can write:

export const handleFilters = (type, val) => {
  return (dispatch, getState) => {
    switch (type) {
    case "input":
      dispatch({
        type: FILTER_BY_INPUT,
        payload: val
      });
    case "time":
      ...
    }
  }
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question