restate / v1.0.0
कंपोनंटच्या मालकीचे स्टेट
प्रत्येक दस्तऐवज, संवाद किंवा विनंतीला स्वतःचे स्टोअर द्या, जे कंपोनंटसोबत तयार होतात आणि तो अनमाउंट झाल्यावर मोकळे होतात.
या पानावर
useRestateScopefactory चे नियमprops मधून सुरुवातीची मूल्येसर्व्हर रेंडरिंगदस्तऐवजाच्या सेव्ह केलेल्या डेटाची वाट पाहणेवापरून पाहाReact बाहेरील मालकuseRestateScope
factory स्टोअर घोषित करून परत करते. restate रेंडरदरम्यान परिणाम टिकवते आणि कंपोनंट अनमाउंट झाल्यावर scope नष्ट करते. बंद करणारे फंक्शन किंवा स्वच्छता effect लिहावे लागत नाही.
दोन Document एलिमेंट्सचे स्टेट वेगवेगळे असते. एक दस्तऐवज मुलांसोबत सामायिक करण्यासाठी, परत केलेले हुक props किंवा तुमच्या स्वतःच्या context द्वारे पाठवा.
import { restate, useRestateScope } from '@kkwaenggwari/restate';
function Document() {
const { useDraft, useSelection } = useRestateScope((scope) => ({
useDraft: restate('', { scope }),
useSelection: restate(0, { scope }),
}));
const text = useDraft();
const selection = useSelection();
return (
<section>
<input value={text} onChange={(event) => useDraft.set(event.target.value)} />
<button onClick={() => useSelection.set((n) => n + 1)}>Selection: {selection}</button>
</section>
);
}factory चे नियम
React factory अनेक वेळा कॉल करून परिणाम टाकून देऊ शकते, त्यामुळे ते synchronous आणि शुद्ध ठेवा.
- स्टोअर factory मध्ये घोषित करा, आणि परत केलेले हुक कंपोनंटच्या वरच्या स्तरावर कॉल करा.
- सुरुवातीचे props useState च्या इनिशियलायझरप्रमाणे एकदा वाचले जातात.
- वेगळ्या दस्तऐवजाला नवीन स्टेट हवे असल्यास कंपोनंटला key द्या.
- factory शिवाय useRestateScope() नंतर effect किंवा हँडलरमध्ये तुम्ही तयार कराल अशा स्टोअरसाठी स्थिर scope परत करते.
props मधून सुरुवातीची मूल्ये
props सुरुवातीची मूल्ये म्हणून वापरा, आणि वेगळ्या दस्तऐवजासह नव्याने सुरू करण्यासाठी React ची key वापरा.
function Editor({ document }) {
// The initial value is captured once, like a useState initializer.
const useTitle = useRestateScope((scope) => restate(document.title, { scope }));
const title = useTitle();
return <input value={title} onChange={(event) => useTitle.set(event.target.value)} />;
}
// A different document gets fresh state.
<Editor key={document.id} document={document} />;सर्व्हर रेंडरिंग
सर्व्हर route मध्ये डेटा लोड करून prop म्हणून पाठवा. सुरुवातीचे मूल्यच सर्व्हर स्नॅपशॉट असते. Next.js App Router मध्ये हा हुक Client Component मध्ये असतो, जो सर्व्हरवरही रेंडर होतो.
'use client';
import { restate, useRestateScope } from '@kkwaenggwari/restate';
export default function Profile({ user }) {
const useProfile = useRestateScope((scope) => restate(user, { scope }));
const profile = useProfile();
return <p>{profile.name}</p>;
}दस्तऐवजाच्या सेव्ह केलेल्या डेटाची वाट पाहणे
scope मधील सेव्ह होणारे स्टोअर क्लायंटवर कंपोनंट commit झाल्यावर पुनर्संचयन सुरू करतात; ते जागतिक restateManager.ready चा भाग नाहीत. लोडिंग सीमेमध्ये scope च्या स्वतःच्या manager.ready ची वाट पाहा.
import { useEffect, useState } from 'react';
import { persistedRestate, useRestateScope } from '@kkwaenggwari/restate';
function Document({ documentId }) {
const { manager, useDraft } = useRestateScope((scope) => ({
manager: scope.manager,
useDraft: persistedRestate('', { scope, namespace: 'documents', key: documentId }),
}));
const [state, setState] = useState('loading');
useEffect(() => {
let active = true;
manager.ready.then(
() => active && setState('ready'),
() => active && setState('error'),
);
return () => {
active = false;
};
}, [manager]);
if (state === 'error') return <p>Could not load this document.</p>;
if (state === 'loading') return <p>Loading document…</p>;
return <Editor useDraft={useDraft} />;
}वापरून पाहा
दस्तऐवज उघडा आणि बंद करा, आणि त्यांचे स्टेट पुन्हा कसे सुरू होते ते पाहा.
उदाहरण तयार होत आहे…
React बाहेरील मालक
स्टेट सेवा किंवा सर्व्हर विनंतीच्या मालकीचे असल्यास, scope स्वतः तयार करा आणि शेवटी destroy() कॉल करा. scope नष्ट केल्याने सेव्ह केलेल्या नोंदी कधीही हटत नाहीत.
import { createRestateScope, persistedRestate } from '@kkwaenggwari/restate';
export function createDocument(documentId) {
const scope = createRestateScope();
const useDraft = persistedRestate('', {
scope,
namespace: 'documents',
key: documentId,
});
return {
useDraft,
manager: scope.manager,
save: () => scope.manager.saveAll(),
destroy: () => scope.destroy(),
};
}