0

I'm unable to push the id with history.push But if I don't add id then it works fine. I'm using react-router-dom v5

useEffect(() => {
  fetchData();
  if (!userInfo) {
    history.push("/login"); // This portion works
  } else if (successCreate) {
    history.push(`/order/${order.id}`); // This does not work as expected
  }
}, [userInfo, history]);

// Here if I don't use order.id then it works, However I can console the order.id and it displays the id properly. Is there any alternative to do the same thing in react. ? Or how to fix this issue

2 Answers 2

1

Problem is, React Hook useEffect has a missing dependency inside the array. You are not adding the successCreate in the dependency array, include it then it will work as you are expecting.

Modify your code to :

useEffect(() => {
  fetchData();
  if (!userInfo) {
    history.push("/login"); // This portion works
  } else if (successCreate) {
    history.push(`/order/${order.id}`); // Now this will work as expected
  }
}, [userInfo, history, successCreate]); // Modifed Line
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks faisal, that's working now.
0

Are you sure that order.id has a value? It could be undefined maybe. Literal string seems to be ok

1 Comment

Yes i have the id as Integer

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.