Q:

React redux read .txt file

I am using React and Redux together.
I have an action file called UploadFileAction.js and here is the code:
export const selectFile = (file) => {
    return {
        type: 'FILE_SELECTED',
        payload: file
    }
};

The file parameter contain the uploaded file.

What I want to do

I want to send the file content to my reducer not the whole file as a payload..

What I have tried

let fileReader = new FileReader();
fileReader.readAsText(file, "UTF-8");
fileReader.onload = function(fileLoadedEvent){
    let textFromFileLoaded = fileLoadedEvent.target.result;
}

Now the textFromFileLoaded variable contain my file text but I couldn't pass it in my payload.

A:

Maybe I am misunderstanding, but you have to dispatch the result of the action containing the file's text to pass it to the reducer. So it should look something like:
 let fileReader = new FileReader();
 fileReader.onload = function(fileLoadedEvent){
    dispatch(selectFile(fileLoadedEvent.target.result));
 } 
 fileReader.readAsText(file, "UTF-8");

further reading on this topic: https://redux.js.org/docs/basics/Actions.html

