React_State_Communication_Component.pptx

EugeneBusico 0 views 8 slides Oct 06, 2025
Slide 1
Slide 1 of 8
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8

About This Presentation

Just an upload


Slide Content

React State Communication Patterns

Parent → Child Props flow down from parent to child. Example: function Child({ message }) { return <h2>{message}</h2>; } function Parent() { return <Child message='Hello from Parent!' />; }

Child → Parent Callbacks lift data back up. Example: function Child({ sendData }) { return <button onClick={() => sendData('Hi Parent!')}>Send</button>; } function Parent() { const [data, setData] = useState(''); return <Child sendData={setData} />; }

Sibling ↔ Sibling Siblings communicate via lifted state in their parent. Example: function Parent() { const [shared, setShared] = useState(''); return <> <SiblingA setShared={setShared} /> <SiblingB shared={shared} /> </>; }

Cross-Tree (Context API) Avoid prop drilling with Context. Example: const UserContext = createContext(); <UserContext.Provider value='Euge'> <Child /> </UserContext.Provider> function GrandChild() { const user = useContext(UserContext); return <h2>{user}</h2>; }

Global State Use Redux, Zustand, or Recoil for app-wide state. Example (Redux): const counterSlice = createSlice({ name: 'counter', ... }); const count = useSelector(state => state.counter.value); const dispatch = useDispatch(); dispatch(increment());

Custom Hooks Reusable state sharing logic across components. Example: function useSharedState() { const [value, setValue] = useState(''); return { value, setValue }; } // Components use shared.value and shared.setValue

Cross-Tab Communication Use localStorage or BroadcastChannel API to sync state across tabs. Example: const bc = new BroadcastChannel('channel'); bc.postMessage('Hello!'); bc.onmessage = (e) => console.log(e.data);
Tags