question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting file data into Redux store

See original GitHub issue

I’m a newbie developer so I hope this is the correct place to put this kind of issue.

I’m using the react-dropzone in a project that is using React-Redux in Electron. I really like it but can’t figure out how to get the file data into my Redux store.

I’m dispatching the file data from my onDrop method but the data isn’t loaded into the Files inside the Filelist, so each one just has the preview key/value.

Here is what my Redux state has for each file: {"preview":"blob:file:///f30eafb8-8a77-4402-9111-379590b4a03f"}

But if I log the file variable to the console, it is fully loaded by the time I view it, so it has all of the data loaded.

{
  lastModified: 1473709614000,
  lastModifiedDate: Mon Sep 12 2016 12:46:54 GMT-0700 (PDT),
  name: "water off a duck's back.jpg",
  path: "/Users/mitchconquer/Desktop/test_image.jpg",
  preview: "blob:file:///0fe69686-125d-464a-a0a8-a42e497d3808",
  size: 41805,
  type: "image/jpeg",
  webkitRelativePath: ""
}

I believe the problem is just that the fiel object isn’t fully loaded, but I can’t figure out a way to only fire the action once the data is fully loaded. Could someone point me on the right path?

Here’s my component that’s using react-dropzone:

// FileDrop.js
import React, { Component, PropTypes } from 'react';
import { Link } from 'react-router';
import styles from './Home.css';
import Dropzone from 'react-dropzone';

export default class FileDrop extends Component {
  static propTypes = {
    message: PropTypes.string.isRequired,
    setFile: PropTypes.func.isRequired
  };

  onDrop(files, event) {
    // console.log({files});
    this.props.setFile(files);
  }

  render() {
    return (
      <div>
        <Dropzone onDropAccepted={this.onDrop.bind(this)} multiple={false} disablePreview={false}>
          <div>{this.props.message}</div>
        </Dropzone>
      </div>
    );
  }
}

…and here’s my action file that has the setFile method that’s used in the above onDrop method:

// actions/files.js
export const SET_FILE = 'SET_FILE';

// ACTION CREATORS

function _setFile(file) {
  return {
    type: SET_FILE,
    file
  };
}

// ACTION CREATOR CREATORS

export function setFile(file) {
  return _setFile(file[0]);
}

…and here’s the reducer that’s handling that action:

// reducers/files.js
import { SET_FILE } from '../actions/files';

export const initialState = [];

export default function files(state = initialState, action = {}) {
  switch (action.type) {
    case SET_FILE:
      const newState = state.slice();
      const file = action.file;
      newState.push(file);
      return newState;
    default:
      return state;
  }
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6

github_iconTop GitHub Comments

6reactions
mitchconquercommented, Dec 19, 2016

Hi, @ghoshnirmalya and @sandervanhooft I posted my findings on Stack Overflow here. I hope they help you!

3reactions
acaceres-conveycommented, May 17, 2017

Hey guys I have a question similar to this one… when i drop a file i don’t get the file path… all i get is lastModified: 1471436812784 lastModifiedDate: Wed Aug 17 2016 07:26:52 GMT-0500 (Central Daylight Time) name: "AATDB" preview: "blob:http://localhost:3000/6b3e96a2-93f9-4107-b539-e244104d2acf" size: 4840 type: "" webkitRelativePath: ""

Read more comments on GitHub >

github_iconTop Results From Across the Web

Accessing the Store | React Redux
Accessing the Store. React Redux provides APIs that allow your components to dispatch actions and subscribe to data updates from the store.
Read more >
How to store the uploaded value into redux store
First off, it looks like you have all the data you need to get the JSON you are looking for. You need to...
Read more >
Add Large Amounts of Data in the App State in Redux
Redux stores can store unlimited amounts of data usable across multiple components. This guide covers adding large amounts of data in a ...
Read more >
Getting Data Out of the Redux Store with Selectors
// You can use createPropsSelector within actions and commands to extract data from the state. This will ensure that you're always working with...
Read more >
Persisting Files in Javascript (React) Applications
LocalStorage is a property that allows web applications to store data locally within the user's browser as key/value pairs with no expiration date....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found