You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For more information about custom renderers, have a look [here](/docs/tutorial/custom-renderers).
116
121
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?
118
155
119
156
We use Ajv for handling JSON Schema's default values.
120
157
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