Oliver Zeigermann / @DJCordhose
Online version at: http://bit.ly/1AS7bQz
Copyright 2015, Oliver Zeigermann
Copyright 2015, Oliver Zeigermann
class HelloMessage extends React.Component {
render() {
return (
{this.state.greeting}, World
);
}
constructor(props) {
super(props);
this.state = {greeting: this.props.greeting};
}
updateModel(event) {
this.setState({greeting: event.target.value});
}
reset() {
this.setState({greeting: ""});
}
}
var mountNode = document.getElementById('example');
React.render(<HelloMessage greeting="Hello" />, mountNode);
Run
Copyright 2015, Facebook, http://facebook.github.io/flux/docs/overview.html
Copyright 2015, Facebook, http://facebook.github.io/flux/docs/overview.html
Copyright 2015, Facebook, http://facebook.github.io/flux/docs/overview.html
Copyright 2015, Facebook, http://facebook.github.io/flux/docs/overview.html
Copyright 2015, Facebook, http://facebook.github.io/flux/docs/overview.html
class HelloMessage extends React.Component {
render() {
return (
{this.state.greeting}, World
);
}
constructor(props) {
super(props);
this.state = {greeting: this.props.greeting};
}
updateModel(event) {
this.setState({greeting: event.target.value});
}
reset() {
this.setState({greeting: ""});
}
}
var mountNode = document.getElementById('example');
React.render(<HelloMessage greeting="Hello" />, mountNode);
class HelloMessage extends React.Component {
render() {
return (
// `this.state.greeting` turns `this.props.greeting`
{this.props.greeting}, World
);
}
updateModel(event) {
// What do we do here???
// this.setState({greeting: event.target.value});
}
reset() {
// What do we do here???
// this.setState({greeting: ""});
}
}
// close to what is called action creator in Flux notation
const messageAction = (actionContext, messagePayload) => {
actionContext.dispatch('MESSAGE_ACTION', messagePayload);
}
class HelloMessage extends React.Component {
render() {
// unchanged
}
reset() {
this.context.executeAction(messageAction, '');
}
updateModel(event) {
this.context.executeAction(messageAction, event.target.value);
}
}
class MessageStore extends BaseStore {
static get storeName() {
return 'MessageStore';
}
static get handlers() {
return {
'MESSAGE_ACTION': 'handleMessage'
};
}
handleMessage(payload) {
this._message = payload;
this.emitChange();
}
get message() {
return this._message;
}
}
// causes a re-rendering of component `HelloMessage`
// when one of the stores calls `this.emitChange()`
connectToStores(HelloMessage, [MessageStore], getStateFromStores);
// will be called to create props passed used for re-rendering
const getStateFromStores = (stores) => {
return {
greeting: stores.MessageStore.message
};
});
class HelloMessage extends React.Component {
render() {
return (
// child views
<MessageDisplay message={this.props.greeting + ', World'}/>
<ResetButton resetHandler={this.reset.bind(this)} />
);
// rest unchanged
}
class MessageDisplay extends React.Component {
render() {
return {this.props.message}
;
}
}
class ResetButton extends React.Component {
render() {
return (
);
}
}
Oliver Zeigermann / @DJCordhose