Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Flux: Error with AppDispatcher.register

I am trying to set up the most basic app in Flux-React. Its sole goal to is fire an Action, which gets sent through the Dispatcher to a Store that has registered with the Dispatcher. The store the logs the payload to Console.

Everything besides the Store is working well, but as soon as it hits AppDispatcher.register, Flux throws the following error:

Uncaught TypeError: Cannot set property 'ID_1' of undefined

Here is the code of the file causing the error, but I've put up the entire project at https://github.com/bengrunfeld/react-flux-dispatcher-error, and you can find the offending file in src/js/stores/AppStores.js

var AppDispatcher = require('../dispatcher/AppDispatcher');
var EventEmitter = require('events').EventEmitter;
var AppConstants = require('../constants/AppConstants');
var assign = require('object-assign');


var CHANGE_EVENT = 'change';

var AppStore = assign({}, EventEmitter.prototype, {
  emitChange: function() {
    this.emit(CHANGE_EVENT);
  }
});

AppDispatcher.register(function(payload){
  console.log(payload);
  return true;
})

module.exports = AppStore;
like image 851
Ben Avatar asked Mar 03 '15 02:03

Ben


People also ask

Can a change in flux be listened by a React component?

So using Flux with React, your store can just emit change without any details and the views that listen to it can just render with 'optimal' Dom manipulation (so if it's state hasn't been changed, there will be no Dom manipulation).

How do you implement flux in React?

Go to the appDispatcher. js and copy-paste the following code: import { Dispatcher } from "flux"; const dispatcher = new Dispatcher(); export default dispatcher; Here we are importing the Dispatcher from the flux library that we installed, creating a new object and exporting it so that our actions module can use it.

How flux works in React?

Flux is a Javascript architecture or pattern for UI which runs on a unidirectional data flow and has a centralized dispatcher. It is useful when your project has dynamic data and you need to keep the data updated in an effective manner. It was created by Facebook, and complements React as view.

Does React use flux?

Flux is a pattern for managing how data flows through a React application. As we've seen, the preferred method of working with React components is through passing data from one parent component to it's children components. The Flux pattern makes this model the default method for handling data.


1 Answers

Because of the drought of documentation of biblical proportions for Facebook Flux, I didn't know that I was using code from previous versions.

In AppDispatcher.js, you need to define AppDispatcher in the following way using the new keyword:

var Dispatcher = require('flux').Dispatcher;
var assign = require('object-assign');

var AppDispatcher = assign(new Dispatcher(), {
  handleViewAction: function(action) {
    this.dispatch({
      source: 'VIEW_ACTION',
      action: action
    });
  }
});

module.exports = AppDispatcher;

Here is a link to a repository with the working code: https://github.com/bengrunfeld/react-flux-simple-app

like image 95
Ben Avatar answered Sep 21 '22 16:09

Ben