Answer the question
In order to leave comments, you need to log in
How to combine react and node_js?
Hello. Please tell me:
there is an application in react with the output of images from a json file. each image has a rating. when you click on the image, the rating increases by 1. You need to write a new rating to the file.
entry file
const fs = require('fs').promises;
export function writeFileJson(data) {
return fs.writeFile('result.json', data);
}
file on function call:
import React, { Component } from 'react';
import { writeFileJson } from '../WriteRaiting/WriteRaiting';
....
writeFileJson(updateList);
....
gives the error Cannot read property 'writeFile' of undefined
but if you run the file with writing through the command line: node ./src/components/WriteRaiting/WriteRaiting.js the file is created and written.
Can you please tell me how to combine a file with a record (nodeJs) with a react application?
Thank you very much in advance for your time. Have a good time
Answer the question
In order to leave comments, you need to log in
Dear comrade in the workshop, react is a frontend application, which means that the code that you write in it will be executed in the browser, therefore the browser cannot read / write to files. For such needs, node.js was invented. This is a separate application, or rather a server, and now it can access the computer's file system and knows how to work with it. Read about node.js and how to work with it and then you will understand everything.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question