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.
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.