0

I have a class:

export default class Home extends React.Component {
    static store = createStore();

    constructor() {
        super();
        // This doesn't work
        console.log(this.store);
    }
}

and I want to be able to access the store variable defined at the top of the class however I'm not sure how, I had assumed it was by using this.store but it is undefined.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
rcjsdev
  • 813
  • 2
  • 9
  • 20
  • `this` is an instance, not your class. It doesn't have a `store` property? Have a look at [this question](http://stackoverflow.com/q/28627908/1048572) – Bergi Mar 03 '16 at 20:53

1 Answers1

-1

So basically you want to share the variable between all class instances? Try to pass it to constructor. Something like this:

class Home extends React.Component {
  constructor(props, context) {
    super(props, context);
    this.store = props.store;

    console.log(this.store);
  }
}

your init function:

function init() {

  var props = {
    store: createStore()
  };

  ReactDOM.render(<Home {...props} />, document.getElementById('home1'));
  ReactDOM.render(<Home {...props} />, document.getElementById('home2'));
  ReactDOM.render(<Home {...props} />, document.getElementById('home3'));
}

and html:

<div id="home1"></div>
<div id="home2"></div>
<div id="home3"></div>
Roman Pushkin
  • 5,639
  • 3
  • 40
  • 58