@ai-stack/payloadcms
Version:
<p align="center"> <img alt="Payload AI Plugin" src="assets/payload-ai-intro.gif" width="100%" /> </p>
21 lines (20 loc) • 838 B
JSX
import React, { createContext } from 'react';
const initialContext = {
field: undefined,
path: '',
schemaPath: '',
};
export const FieldContext = createContext(initialContext);
export const FieldProvider = ({ children, context, }) => {
// `context` is already current on every render (it's rebuilt by the caller from live
// field props), so there's no need to mirror it into local state - doing so previously
// caused `field` to get stuck at whatever value it had on the first render for a given
// `schemaPath`, including `undefined` if it wasn't populated yet at that point.
return (<FieldContext.Provider value={{
field: context.field,
path: context.path,
schemaPath: String(context.schemaPath ?? ''),
}}>
{children}
</FieldContext.Provider>);
};