React Context API ile Global State Yönetimi
React Context API, uygulamanızdaki verilerin bir bileşen hiyerarşisi boyunca paylaşılmasını sağlar. Global state yönetimi için aşağıdaki adımları izleyebilirsiniz:1. Context Oluşturma
- React'ten `createContext` fonksiyonunu kullanarak bir context oluşturun. ```javascript import React, { createContext, useState } from 'react'; const MyContext = createContext(); ```2. Provider Oluşturma
- Oluşturduğunuz context'i kullanarak bir Provider bileşeni oluşturun. Global durumu burada saklayabilirsiniz. ```javascript const MyProvider = ({ children }) => { const [state, setState] = useState(initialValue); return (3. Uygulama Yapısına Entegre Etme
- `MyProvider` bileşenini uygulamanızın üst seviyesinde yerleştirin. Böylece tüm alt bileşenler bu state'e erişebilir. ```javascript const App = () => (4. State Kullanma
- Context'ten veriye erişmek için `useContext` hook'unu kullanın. ```javascript import React, { useContext } from 'react'; const YourComponent = () => { const { state, setState } = useContext(MyContext); return ({state}
Özet
- Context oluşturun.
- Provider ile global durumu yönetin.
- Provider'ı uygulama yapısına entegre edin.
- Bileşenlerde durumu kullanmak için useContext kullanın.