I have a react app and my page makes request for the current status from react-effector…
- User
I have a react app and my page makes request for the current status from react-effector store on load through event that is trighered using React.useEffect at the start. It works fine, however if I am on the other page and press on the button to navigate to my page with a mouse wheel it opens the page in the new tab but doesn't invoke the event. Why is it so and what can I do to trigger it?
- Assistant
When you navigate to a page with a mouse wheel click, it does not trigger the
useEffecthook in your React component because the page is being opened in a new tab rather than being re-rendered within the existing tab.To trigger the event when navigating to the page in a new tab, you can use a different approach:
-
Use URL parameters: When navigating to the page with the mouse wheel click, you can pass a specific URL parameter that indicates that the event needs to be triggered. In your component, you can then check for this parameter in the
useEffecthook and trigger the event accordingly. -
Use local storage: You can store a flag in the local storage when the event needs to be triggered. When the new page is loaded, you can check for this flag in the
useEffecthook and trigger the event if the flag is set. -
Use custom events: You can use custom events to trigger the event when navigating to the page in a new tab. Dispatch a custom event in the previous page when navigating to the new page, and listen for this event in the new page to trigger the event.
Overall, choose an approach that best fits your application's requirements and ensure that the event is triggered when navigating to the page in a new tab.
-
- User
I can ask for the flag on the start of another page using useEffect however it is still in another tab and I can't really trigger it outside
- Assistant
If you need to trigger an event in one tab based on an action in another tab, you can use
localStorageand thestorageevent. Here's an example of how you can achieve this:In Tab A (the page where the mouse wheel click happens), set a flag in
localStorage:jsx// Tab A // Set a flag in localStorage when the mouse wheel click happens localStorage.setItem('triggerEvent', 'true');In Tab B (the page you want to trigger the event on), listen for the
storageevent and check for the flag inlocalStorage:jsx - User
can it be done using websocket?
- Assistant
Yes, you can also achieve communication between tabs using websockets. Here's how you can do it:
-
Set up a WebSocket server: You will need to set up a WebSocket server that both tabs can connect to. This server will facilitate communication between tabs.
-
Connect tabs to the WebSocket server: In each tab, establish a WebSocket connection to the server. You can do this using the
WebSocketAPI in JavaScript.
Tab A - Emit event to trigger in Tab B:
jsxconst ws = new WebSocket('ws://your-websocket-server-url'); // Emit an event to trigger in Tab B ws.send('triggerEvent'); // Close the WebSocket connection ws.close();Tab B - Listen for events triggered by Tab A:
jsx -