-2

Example, when I use the link:

https://stackoverflow.com/questions/11227809

It automatically converts to:

https://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster...

How to do the same thing in Reactjs? (I already have a string inside my Component)

3
  • It's temporary redirect in server level. In react: window.location.href = window.location.href + '/string-that-you-already-have' Commented Aug 6, 2021 at 11:41
  • 1
    You can refer the window.location.href explanation here w3schools.com/howto/howto_js_get_url.asp, Also you can refer here for more documentation developer.mozilla.org/en-US/docs/Web/API/Window/location Commented Aug 6, 2021 at 11:43
  • @Justinas not sure that this is the way to go in a React context tbh, at all. Commented Aug 6, 2021 at 12:36

2 Answers 2

2

Generally:

To redirect the client (that is, to basically simulate them clicking a link), use window.location.href = 'my-domain/my-subdirectory'.

To replace the client's URL (that is, to redirect them without adding the current URL to the browser history), use window.location.replace('my-domain/my-subdirectory').

Sign up to request clarification or add additional context in comments.

Comments

1

My answer:

  const { id } = useParams(); // get id by react-route
  const history = useHistory();

  useEffect(() => {
    let exampleName = "string-by-id";  // get string name by id 
    history.push("/questions/"+id+"/"+exampleName);
  }, []);

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.