#Context between Siblings Components

8 messages · Page 1 of 1 (latest)

spring trench
#

I want to from a child change Parent context

woven bear
#

just have a component C that wrap both ?

spring trench
spring trench
spring trench
#

iT'S more what i want to do

Do i use Context ?

#
// GrandParent.tsx
import { $, component$, type QRL, useStore } from "@builder.io/qwik";

export const grandParent = component$(() => {
  const dataStore = useStore({ message: "" });

  const handleDataFromChild = $((data: string) => {
    dataStore.message = data; 
  });
  return (
    <div>
      <h1>data received from child : {dataStore.message}</h1>
      <Parent onReceiveData={handleDataFromChild} />
    </div>
  );
});

interface ParentProps {
  onReceiveData: QRL<(data: string) => void>;
}

export const Parent = component$(({ onReceiveData }: ParentProps) => {
  return <Child sendDataToGrandParent={onReceiveData} />;
});

interface ChildProps {
  sendDataToGrandParent: QRL<(data: string) => void>;
}

export const Child = component$(({ sendDataToGrandParent }: ChildProps) => {
  const sendData = $(() => {
    sendDataToGrandParent("i child (Qwik)!");
  });
  return (
    <button type="button" onClick$={sendData}>
      Send
    </button>
  );
});