» npm install jsoneditor-react
Videos
» npm install json-edit-react
» npm install react-json-editor-ajrm
You can use some JSON editor like react-ace
Put your json file in assets, then import it to your component and follow the guide on the README! Your component would look like:
Copyconst [text, setText] = useState(JSON.stringify(yourJsonFile, null, 2));
function handleChange(text, event) {
try {
setText(text && JSON.parse(text));
} catch (error) {
// pass, user is editing
}
}
return (
<AceEditor
mode="java"
theme="github"
onChange={handleChange}
name="UNIQUE_ID_OF_DIV"
editorProps={{ $blockScrolling: true }}
value={text}
/>
);
In a React project, all your source code and assets are in the src folder. The compiler will put everything you need in the public folder. If the JSON file is not copied, you might need a plugin that copies static files to the public folder.
About your other question: do you want the user to be able to edit the JSON as a text string? Then you can just load the JSON and keep it as a string. That string you could put into some text input field. It could look something like this:
Copyfetch('assets/myfile.json')
.then(responseString => {
myEditableTextField.value = responseString
})
To store the results after the user edited it, you will still need some kind of server page (NodeJS or PHP) that can save the file back to a JSON file after editing.
But note this is still bad practice. There is an extremely high chance that the JSON will not be correct after the user has edited it.
It would be better to build some kind of CMS that edits all the JSON properties separately.
Copyfetch('assets/myfile.json')
.then(responseString => responseString.json())
.then(data => {
myEditablenameField.value = data.name
// etc. for all values in the json file
})
» npm install react-json-editor