Search code examples
javascriptreactjsreact-routerbrowser-historyreact-router-v4

react-router browserHistory.listen unlisten


How can I unlisten to that route? My function "execute" keeps running one Im cliking on links

const unlisten = browserHistory.listen(unlis);

function handleUrl(location) { const newPath = location.pathname; if (!newPath.includes(${id}))) { execute(); unlisten(window.location); } else unlisten(window.location); browserHistory.listen(handleUrl); handleUrl(window.location);

Ive tried to do:

  browserHistory.push(`/search/${id}');

  const unlisten = browserHistory.listen(location);

  function handleUrl(location) {
    const newPath = location.pathname;
    if (!newPath.includes(`${id})`) {
      execute();
      unlisten(window.location);
    else unliste;
  browserHistory.listen(handleUrl);
  handleUrl(window.location);

but no luck


Solution

  • Find out how to fix it

      let unlisten;
      function handleUrl(location) {
        const newPath = location.pathname;
        if (!newPath.includes(`${id})`)) {
          execute();
          unlisten();
        }
      unlisten = browserHistory.listen(handleUrl);
      handleUrl(window.location);
    }