I have a JSON
object which is deeply nested. I want to display input fields with respect to the JSON values. To traverse through the deeply nested JSON object I used recursion and collected all processed input fields to set it into a state to display in the main return of the module. However, it is throwing Too many re-renders to infinite loop. Uncaught run time errors
.
I have tried various methods such as,
-
Storing
displayFrom
indisplayProperties
in a separate const and used thatconst
intosetDisplayForm
. -
Applied various conditions in the main return of App module such as
typeof properties[ key ] === 'object'
in the conditional rendering -
Used
useMemo
to wrap thedisplayProperties
function from causing too many re-render issue.
I have implemented the same in stackblitz please take a look at my code and comment. https://stackblitz.com/edit/vitejs-vite-kjyz2y?file=src%2FApp.jsx&terminal=dev
There is an const input
it makes the code looks bigger and complex but it is just a JSON nested object. The actual logic and code looks like below,
import { useState } from 'react';
import reactLogo from './assets/react.svg';
import viteLogo from '/vite.svg';
import './App.css';
function App() {
const [displayFrom, setDisplayForm] = useState();
const input = {} // a deeply nested JSON object
const displayProperties = (properties) => {
return (
<>
{Object.keys(properties).map((key, index) => (
<div key={index}>
<span>{key === 'label' ? properties[key] : null}</span>
<span>
{JSON.stringify(properties[key]).includes('properties') ||
JSON.stringify(properties[key]).includes('label')
? displayProperties(properties[key])
: null}
</span>
</div>
))}
</>
);
};
const builtForm = () => displayProperties(input);
setDisplayForm(builtForm);
return <>{displayFrom}</>;
}
export default App;
2
Answers
The problem is likely due to the fact that you can’t use an index as a key value.
Try something like this:
Here is a preview of the app we will make. The user can supply any arbitrary object as a starting point. As the user changes values in the form, the deeply nested object is updated –
an isomorphism first
In another Q&A we go through the process of flattening and unflattening any nested object using a
fromJS
andtoJS
interface. The details of that process will not be covered in this post. We start by flattening the object –And
toJS
can reverse the operation –react component
Now we make a React
Form
component to operate on the flat state. Run the code example below and watch the nested object update –remarks
In ~20 lines of React, we made our first Formik clone. You can see how the
Form
component could be extended to render a more sophisticated HTML representation of the nested values, or to allow for the addition/removal of new fields. Once the input object is represented as flat state, the challenges of working with it in React are greatly reduced.To make the demo run online, all dependencies are included in this small code paste. Separate the generic functions into their respective modules to keep the React component focused on its task.