0

I have a requirement to be implemented where- when the form is loaded, basis one of the attribute which contains a url link - a new browser tab should be auto opened with that url when form is loaded.

So i checked few stakc overflow links and found one solution Maintaining href "open in new tab" with an onClick handler in React

      const openInNewTab = (url) => {
        const newWindow = window.open(url, '_blank', 'noopener,noreferrer')
        if (newWindow) newWindow.opener = null
      }


       return (
            <ul>
            {dataset.map((x, id) => (
                <li 
                key={id}
                >
                    <b>{x.split('~')[0]}</b>&ensp;-&ensp;{checkString(x.split('~')[1])}
                    {x.split('~')[0]==='PAGE_URL'  ?  
                        openInNewTab(x.split('~')[1])
                    :
                    <></>
                }
                </li>
               
            ))}
            </ul>
        );

So with above code, when my form is loaded, it immediately loads new tab on broswer as expected but the issue here is whenever i try to type in other form questions or click on any button like radio button on that form, everytime it is opening new tab which i don't want. How do i prevent multiple times auto pop up of new tab or is there any other solution? Please help with solution or alternatives.

I mentioned whever i tried in above

Abcd Efgh
  • 57
  • 5
  • can you provide a working example? – yograj tandel Jan 12 '23 at 06:08
  • Okay, I click on "Get tasks" button It will display the fetch details which has attributes like PAGE_URL(which is one of them) and some form questions on right side.The new tab with url opens up immediately with the details i want to see. Now i go back to my website and want to fill in the form questions. Everytime I click on a raio button in the form or enter a text in the text box, it is opening up that new tab which i dont want. I want it opened up only once when the task is fetched. Is this clear now – Abcd Efgh Jan 12 '23 at 06:15
  • Okay, I click on "Get tasks" button It will display the fetch details which has attributes like PAGE_URL(which is one of them) and some form questions on right side.The new tab with url opens up immediately with the details i want to see. Now i go back to my website and want to fill in the form questions. Everytime I click on a raio button in the form or enter a text in the text box, it is opening up that new tab which i dont want. I want it opened up only once when the task is fetched. Is this clear now – Abcd Efgh Jan 12 '23 at 06:17
  • I have to see the working example to debug it and give the solution. so it's better if you add a code snippet in your question. – yograj tandel Jan 12 '23 at 06:23
  • Yograj, that is the only code i added – Abcd Efgh Jan 12 '23 at 06:44

1 Answers1

0

You can call the openInNewTab function from a useEffect hook. The reason a new tab is opened when ever you type in something in the input field is because react is re-rendering that component due to state changes. Since you want this function to be called only once, you can move the logic of calling this function to a useEffect hook.

Ghouse Mohamed
  • 387
  • 4
  • 10
  • Can you help me with this? Like how will useEffect enable the new tab to be opened only first time and not other times? not at all good with recat – Abcd Efgh Jan 12 '23 at 06:47
  • useEffect with an empty dependency array will be always called once before the component renders. Because of this the openInNewTab function will be called only once. – Ghouse Mohamed Jan 12 '23 at 06:52
  • let me try ghouse, ill get back to you – Abcd Efgh Jan 12 '23 at 06:58
  • hi ghouse, i added it like this: ` {obj.split('~')[0]==='LANDING_PAGE_URL' ? useEffect(() => {openInNewTab(obj.split('~')[1])},[] ) : <>> } ` But getting error: React Hook "useEffect" cannot be called inside a callback. React Hooks must be called in a React function component or a custom React Hook function react-hooks/rules-of-hooks – Abcd Efgh Jan 12 '23 at 07:05