In the React app I click on button to open new tab. When I open new tab I want to pass some data to the new tab.
Code from my button component
const channel = new BroadcastChannel('color');
const handleClick = () => {
...
channel.postMessage('GREEEENNNNNNNNN');
}
Code from new tab component
const [color, setColor] = React.useState('');
const channel = new BroadcastChannel('color');
React.useEffect(() => {
channel.onmessage = (msg) => setColor(msg.data);
}, [channel]);
console.log('color <========', color); // just to see that this is working
What I expect: I see 'GREEEENNNNNNNNN' in the console in new tab
What happening:
- If I open only one new tab, I DON'T see 'GREEEENNNNNNNNN' in the console
- If I open another new tab, I can see 'GREEEENNNNNNNNN' in the console of the first tab
Question: How to broadcast this value to new tab?
To declare the Chanel
To receive message
To send message