Skip to content

Commit 97721f5

Browse files
LukasBollsdirix
andauthored
feat: add data prop and rerender FAQ
closes #214 Co-authored-by: Stefan Dirix <sdirix@eclipsesource.com>
1 parent ff1f44f commit 97721f5

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

content/faq/faq.mdx

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ const uischema = person.uischema;
6161
const initialData = person.data;
6262

6363
function App() {
64-
const [stateData, setData] = useState(initialData);
64+
const [data, setData] = useState(initialData);
6565

6666
return (
6767
<div className="App">
6868
<JsonForms
6969
schema={schema}
7070
uischema={uischema}
71-
data={initialData}
71+
data={data}
7272
renderers={materialRenderers}
7373
onChange={({ errors, data }) => setData(data)}
7474
/>
@@ -100,21 +100,58 @@ const RatingControl = ({ data, handleChange, path }) => (
100100
/>
101101
);
102102

103+
// renderers should be static or memoized
104+
const renderers = [
105+
...materialRenderers,
106+
//register custom renderer
107+
{ tester: rankWith(3,scopeEndsWith('rating')), renderer: RatingControl }
108+
]
109+
110+
...
111+
103112
<JsonForms
104113
schema={schema}
105114
uischema={uischema}
106115
data={stateData}
107-
renderers={[
108-
...materialRenderers,
109-
//register custom renderer
110-
{ tester: rankWith(3,scopeEndsWith('rating')), renderer: RatingControl }
111-
]}
116+
renderers={renderers}
112117
/>
113118
```
114119

115120
For more information about custom renderers, have a look [here](/docs/tutorial/custom-renderers).
116121

117-
## How can I use default values within JSON Forms
122+
## How can I minimize re-rendering?
123+
124+
JSON Forms uses `React.memo` to avoid any unnecessary re-rendering.
125+
Therefore props should in general be stable, for example by memoizing them.
126+
There are three exceptions: JSON Forms can handle "onChange" and "middleware" changes, for example to support anonymous functions. Also new "i18n" objects will be ignored as long as their properties are stable.
127+
128+
JSON Forms is able to recognize `data` objects it emitted via `onChange`, so handing them over is safe too.
129+
However whenever a different `data` object is handed over, JSON Forms will revalidate and rerender.
130+
131+
```js
132+
const [data, setData] = useState(initialData);
133+
134+
<JsonForms
135+
data={data}
136+
onChange={({ data }) => setData(data)}
137+
/>
138+
```
139+
In this scenario, onChange will set the data in the parent component.
140+
Afterwards the data is passed on to JSON Forms, but JSON Forms will not revalidate and render again, since the data prop is the object emitted by the onChange method.
141+
On the other hand an anti pattern can be seen when looking at the `data` prop in the following example:
142+
143+
```js
144+
const [data, setData] = useState(initialData);
145+
146+
<JsonForms
147+
data={data}
148+
onChange={({ data }) => setData({ ...data })}
149+
/>
150+
```
151+
Updating the state with a new object in the `onChange` function leads to a new render cycle, in which JSON Forms will revalidate the data and retrigger the onChange method, resulting in an endless loop.
152+
Only provide a new object to JSON Forms if necessary, for example if the data was modified outside of JSON Forms.
153+
154+
## How can I use default values within JSON Forms?
118155

119156
We use Ajv for handling JSON Schema's default values.
120157
To enable the creation of default values, you need to create a custom Ajv instance and hand it over to JSON Forms.

0 commit comments

Comments
 (0)