最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to edit a json file in react - Stack Overflow

programmeradmin0浏览0评论

Ok so the pany I'm working in, wants a blog feature on their React website and all of the information from that blog will be stored inside a JSON file. My problem right now is that they also want the React app to allow to edit the JSON file containing all of the information regarding the blog, through the front end. I've tried to change the location of the JSON file to see if that would allow me to edit it, but I can't edit the file whether it's on the public folder or on the src folder. I'm kind of new to react and I don't know exactly where to store the JSON file, in which folder should I store it? And how do I edit the file?

Ok so the pany I'm working in, wants a blog feature on their React website and all of the information from that blog will be stored inside a JSON file. My problem right now is that they also want the React app to allow to edit the JSON file containing all of the information regarding the blog, through the front end. I've tried to change the location of the JSON file to see if that would allow me to edit it, but I can't edit the file whether it's on the public folder or on the src folder. I'm kind of new to react and I don't know exactly where to store the JSON file, in which folder should I store it? And how do I edit the file?

Share Improve this question edited Feb 25, 2021 at 9:57 phuzi 13.1k4 gold badges29 silver badges59 bronze badges asked Feb 25, 2021 at 9:54 Bruno OliveiraBruno Oliveira 3011 gold badge2 silver badges11 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 1

You can use some JSON editor like react-ace

Put your json file in assets, then import it to your ponent and follow the guide on the README! Your ponent would look like:

const [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 piler 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:

fetch('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.

fetch('assets/myfile.json')
    .then(responseString => responseString.json())
    .then(data => {
        myEditablenameField.value = data.name
        // etc. for all values in the json file
    })

It would be easy for you, if you load JSON directly into your React app as a response from API call. Instead of downloading .json file.

axios.get("www.website./json").then((response)=>{
   let someData = JSON.parse(response);
   // do whatever you want to do with 'someData'
   // you could make post request to save edited data
   axios.post("www.website./save-json", {editedJSON: JSON.stringify(someData)})

});


发布评论

评论列表(0)

  1. 暂无评论