Slide 26
Slide 26 text
© kaonavi, inc.
// UI State atoms (フォームの状態 )
const countInputAtom = atom('');
const selectedMenuIdAtom = atom(null);
const isSubmittingAtom = atom(false);
// Derived atom で変換処理を分離
const orderPayloadAtom = atom((get) => {
const countInput = get(countInputAtom);
const menuId = get(selectedMenuIdAtom);
// バリデーションと変換をここで実施
if (!menuId || !countInput) return null;
const count = parseInt(countInput, 10);
if (isNaN(count)) return null;
return { foodId: menuId, count };
});
Jotaiによる実装
26
🏪状態管理による関心ごと分離の実装
// API連携は別のatomで管理
const submitOrderAtom = atom(null, async (get, set)
=> {
const payload = get(orderPayloadAtom);
if (!payload) throw new Error('Invalid form');
set(isSubmittingAtom, true);
const result = await api.submitOrder(payload);
set(isSubmittingAtom, false);
return result;
});