-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)

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

2 Answers2

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').

David F. B.
  • 73
  • 1
  • 8
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);
  }, []);