0

I'm trying to call a function from React JSX button and I have a problem with that because when in react-redux actions in the defined function I don't put dispatch function, works as it supposed to.

const like_function = (id) => {
      let post_id = id;

  if (isAuthenticated) {
    console.log(post_id, user_id);
    like_post(post_id, user_id);
  } else {
    <Redirect to="/login" />;
    console.log("Redirect to login");
  }
};

Here in this button I invoke function with one parameter.

<button onClick={() => like_function(post.id)}>Like</button>

This is redux action. Here is the problem. When dispatch is deleted function works but with dispatch is not even called, it wont even log data to console before async request

export const like_post = (post_id, user_id) => async (dispatch) => {
  const data = { post_id: post_id, user_id: user_id };
  console.log(data);
  dispatch({
    type: POST_LIKE_LOADING,
  });

  try {
    const res = await axios.put(`http://localhost:8000/api/like_list/`, data);
    //console.log(res.data);
    dispatch({
      type: POST_LIKED,
      payload: res.data,
    });
  } catch (err) {
    console.log(err);
    dispatch({
      type: POST_LIKEING_FAIL,
    });
  }
};

Here are my redux reducers

case POST_LIKE_LOADING:
  return {
    ...state,
    isLoading: true,
  };
case POST_LIKED:
  return {
    ...state,
    isLoading: true,
    message: "OK"
  };

Sorry about my English, hope you understood me, thanks in advance

1 Answer 1

2

You're not dispatching the action.

import { useDispatch } from 'react-redux'

const dispatch = useDispatch()

dispatch(like_post(post_id, user_id))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.