- Jun 2023
-
-
```js const authChannel = new BroadcastChannel('authChannel');
authChannel.addEventListener('message', () => { logout(); })
export function logout() { // logout logic }
logoutButton.addEventListener('click', () => { authChannel.postMessage('logout'); logout(); }); ```
```js import { render } from 'solid-js/web'; import { createSignal, For, onCleanup } from 'solid-js';
const todosChannel = new BroadcastChannel('todos');
function Todos() { const listener = todosChannel.addEventListener('message', e => { setTodos(e.data) });
const [todos, setTodos] = createSignal(['One']);
const addTodo = () => { setTodos(todos => [...todos, Math.random().toString()]); todosChannel.postMessage(todos()); }
onCleanup(() => { todosChannel.removeEventListener('message', listener); })
return ( <> <button onClick={addTodo}>Add Todo</button>
<For each={todos()}> {(item) => <div>{item}</div>} </For>
) }
render(() => <Todos />, document.getElementById('root')); ```
-
- Aug 2022
-
developer.mozilla.org developer.mozilla.org