score:1
typo in:
const [parsedFile, setParsedFile] = useState(null);
file reader code:
const fileReader = new FileReader();
fileReader.readAsText(e.dataTransfer.files[0]);
fileReader.onload = () => {
setParsedFile(fileReader.result);
};
score:1
You are trying to print parsedFile
, but in the state is it called parsedFiled
. You are also printing the value before the onload
callback sets the state. Try moving the print into the callback function.
The onload
callback function is asyncronous, so you can't use the value immediately after invoking it because there is no guarantee that the function has resolved. In order to use the file contents, you can do one of the following: Place the relevant code in the render function so it can react to the state change; write code and insert it into the callback function so that it is called when the contents are available; use a syncronous read function or wrap the asyncronous function in a promise, so that you can have an expectation about how it resolves (e.g. await
the promise and use the contents immediately after).
Source: stackoverflow.com
Related Query
- read JSON file from local hard drive and parse into react app
- loading json data from local file into React JS
- How to parse a JSON object from an external file in React using .map() in ES6 and a loop?
- How do I return a JSON array in React from a local file and have it map through all of my items?
- Load Json data from local file into Typescript variable in REACT
- How to read json file from my src folder in my react app using webpack-dev-server?
- Delay loading json data from local file into React JS
- How can I parse through local JSON file in React js?
- You need to enable javascript to run this app response when try to access local json file in react
- How can I get data from a local file into my React app?
- Converting Object into JSON and downloading as a .json file in React
- How to read console.log from a mounted component with Enzyme and Jest in Create React App
- How to get data from local json file using actions and axios.get() (Redux)?
- How to open a text file and read the contents from it on react native?
- Import TypeScript modules from local module into React app
- Loading Sass and injecting CSS text into a parent document from a React app
- How to fetch and display data from json file in react typscript
- How to read and write to local JSON files from React.js?
- Read a File through FileReader Api and Convert it into json object
- Best Way to Render Multiple Paragraph Text read from JSON into React Component
- How can I parse my JSON file into an Object I can manipulate in React js
- React Hook useEffect to fetch data from local json file to create a details page
- Trying to display a random joke, but the question and answers don't match from my local json file
- Create react app with i18next - load translations from json file
- Map inside map in react ( one is local array of images and the other is title from json )
- How to read local html file content inside React js App using Webpack loaders?
- Load any kind of data from a local file in a React app
- Way to read filenames from directory and output to JSON with Electron & React
- Could not fetch local JSON file from public folder in React
- Fetch data from JSON local file in React
More Query from same tag
- Why usePrevious returns undefined on first render - react hooks?
- how to change color of boxes in 2D grid of specific cell on click in reactJs?
- How to theme components with Styled-Component and Material-UI
- How to return child as a function in React after modifying the children with React.Children API?
- Will a large redux state affect my application's performance?
- Reactjs calculate how many pixels in one vw and vh
- How to disable other buttons till the function call is complete after clicking on one in react render()?
- React.useMemo does not update the data
- Add elements to a list that belongs to a state using react
- Fastest way to sort array of objects on the basis of nested key values
- Constructor runs once in multi-Instantiates of a class in "builder pattern" in react app
- How to wait for value before running fetch?
- Validation in Form with React js and Firebase
- How can I update the value of an object while mapping through it's array?
- React JS - Select, Why I'm getting double values printed and stored in the database
- Ternary operator doesn't work as expected
- React-Redux TypeScript Errors
- Rendering a Google Map without react-google-map
- useEffect not called in React Native when back to screen
- How to handle breadcrumbs?
- Nextjs - Styling nested element with css modules
- Ant Design range picker disable array of dates
- Cant override styles of nested Material UI components
- Callback not working in React
- is it possible to show a Button only if admin use?
- nodejs reactjs and webpack, config is wrong but what?
- GraphQL: unexpected variables in variableValues: first when using no variables in args
- how redux works here, i am going through a code base and did not understand how the function under another function dispaches
- redux toolkit +connected-react-router
- webpack4: React babel - You may need an appropriate loader to handle this file type