samedi 3 décembre 2016

Why is the getState() function defined outside of the React component in most flux examples

I'm mostly just curious what the significance of this pattern is. In almost every example I've looked at for using the flux architecture the getAppState() function is defined right above the react class definition. Why? Why is it not a function of the component?

So why is this:

import React from 'react';

getAppState = () => {
  return {
    something: SomeStore.getState()
  }
}

class App extends React.Component {
  constructor(props) {
    super(props);

    this.state = getAppState();
  }
}

Better than this:

import React from 'react';

class App extends React.Component {
  constructor(props) {
    super(props);

    this.state = this.getAppState();
  }

  getAppState() {
    return {
      something: SomeStore.getState()
    }
  }
}

And what if I'm wanting to pass arguments from this.props into the getAppState() function?

Aucun commentaire:

Enregistrer un commentaire