Created
February 6, 2019 14:48
-
-
Save BPagoaga/a979adec26ba48422812a8f972a5e7bd to your computer and use it in GitHub Desktop.
React/Redux snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// action creator | |
const addMessage = (message) => { | |
return { | |
type: 'ADD', | |
message: message | |
} | |
}; | |
const mapStateToProps = (state) => { | |
return { | |
messages: state | |
} | |
}; | |
const mapDispatchToProps = (dispatch) => { | |
return { | |
submitNewMessage: (message) => { | |
dispatch(addMessage(message)); | |
} | |
} | |
}; | |
// component | |
class Presentational extends React.Component { | |
constructor(props) { | |
super(props); | |
} | |
render() { | |
return <h3>This is a Presentational Component</h3> | |
} | |
}; | |
// connect react to redux | |
const connect = ReactRedux.connect; | |
const ConnectedComponent = connect(mapStateToProps, mapDispatchToProps)(Presentational) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Redux: | |
const ADD = 'ADD'; | |
const addMessage = (message) => { | |
return { | |
type: ADD, | |
message: message | |
} | |
}; | |
const messageReducer = (state = [], action) => { | |
switch (action.type) { | |
case ADD: | |
return [ | |
...state, | |
action.message | |
]; | |
default: | |
return state; | |
} | |
}; | |
const store = Redux.createStore(messageReducer); | |
// React: | |
const Provider = ReactRedux.Provider; | |
const connect = ReactRedux.connect; | |
// Change code below this line | |
class Presentational extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
input: '', | |
} | |
this.handleChange = this.handleChange.bind(this); | |
this.submitMessage = this.submitMessage.bind(this); | |
} | |
handleChange(event) { | |
this.setState({ | |
input: event.target.value | |
}); | |
} | |
submitMessage() { | |
this.setState({ | |
input: '' | |
}); | |
this.props.submitNewMessage(this.state.input) | |
} | |
render() { | |
return ( | |
<div> | |
<h2>Type in a new Message:</h2> | |
<input | |
value={this.state.input} | |
onChange={this.handleChange}/><br/> | |
<button onClick={this.submitMessage}>Submit</button> | |
<ul> | |
{this.props.messages.map( (message, idx) => { | |
return ( | |
<li key={idx}>{message}</li> | |
) | |
}) | |
} | |
</ul> | |
</div> | |
); | |
} | |
}; | |
// Change code above this line | |
const mapStateToProps = (state) => { | |
return {messages: state} | |
}; | |
const mapDispatchToProps = (dispatch) => { | |
return { | |
submitNewMessage: (message) => { | |
dispatch(addMessage(message)) | |
} | |
} | |
}; | |
const Container = connect(mapStateToProps, mapDispatchToProps)(Presentational); | |
class AppWrapper extends React.Component { | |
render() { | |
return ( | |
<Provider store={store}> | |
<Container/> | |
</Provider> | |
); | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Redux: | |
const ADD = 'ADD'; | |
const addMessage = (message) => { | |
return { | |
type: ADD, | |
message: message | |
} | |
}; | |
const messageReducer = (state = [], action) => { | |
switch (action.type) { | |
case ADD: | |
return [ | |
...state, | |
action.message | |
]; | |
default: | |
return state; | |
} | |
}; | |
const store = Redux.createStore(messageReducer); | |
// React: | |
class Presentational extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
input: '', | |
messages: [] | |
} | |
this.handleChange = this.handleChange.bind(this); | |
this.submitMessage = this.submitMessage.bind(this); | |
} | |
handleChange(event) { | |
this.setState({ | |
input: event.target.value | |
}); | |
} | |
submitMessage() { | |
const currentMessage = this.state.input; | |
this.setState({ | |
input: '', | |
messages: this.state.messages.concat(currentMessage) | |
}); | |
} | |
render() { | |
return ( | |
<div> | |
<h2>Type in a new Message:</h2> | |
<input | |
value={this.state.input} | |
onChange={this.handleChange} /><br /> | |
<button onClick={this.submitMessage}>Submit</button> | |
<ul> | |
{this.state.messages.map((message, idx) => { | |
return ( | |
<li key={idx}>{message}</li> | |
) | |
}) | |
} | |
</ul> | |
</div> | |
); | |
} | |
}; | |
// React-Redux: | |
const mapStateToProps = (state) => { | |
return { messages: state } | |
}; | |
const mapDispatchToProps = (dispatch) => { | |
return { | |
submitNewMessage: (newMessage) => { | |
dispatch(addMessage(newMessage)) | |
} | |
} | |
}; | |
const Provider = ReactRedux.Provider; | |
const connect = ReactRedux.connect; | |
// define the Container component here: | |
const Container = connect(mapStateToProps, mapDispatchToProps)(Presentational) | |
class AppWrapper extends React.Component { | |
constructor(props) { | |
super(props); | |
} | |
render() { | |
// complete the return statement: | |
return ( | |
<Provider store={store}> | |
<Container /> | |
</Provider> | |
) | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment