Created
July 3, 2018 01:47
-
-
Save JackHowa/9fc9d6a35d5b1c0c3daf38cd9f244c63 to your computer and use it in GitHub Desktop.
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
this is notes for the fcc course |
https://learn.freecodecamp.org/front-end-libraries/react-and-redux/manage-state-locally-first
- can use the
...arr
to spread an array back into itself
class DisplayMessages extends React.Component {
constructor(props) {
super(props);
this.state = {
input: '',
messages: []
}
this.handleChange = this.handleChange.bind(this);
this.submitMessage = this.submitMessage.bind(this);
}
// add handleChange() and submitMessage() methods here
handleChange(event){
this.setState({input: event.target.value});
}
submitMessage(){
this.setState(prevState => {
return {
messages: [
...prevState.messages,
this.state.input],
input: ''
}
})
};
render() {
return (
<div>
<h2>Type in a new Message:</h2>
{ /* render an input, button, and ul here */ }
<input
value={this.state.input}
type={"text"}
onChange={this.handleChange}
/>
<button onClick={this.submitMessage} />
<ul />
{ /* change code above this line */ }
</div>
);
}
};
https://learn.freecodecamp.org/front-end-libraries/react-and-redux/manage-state-locally-first/
this.state.input
is not accessible within setState scope
-> use prevstate to access that shiz
class DisplayMessages 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(){
this.setState(prevState => {
return {
messages: [
...prevState.messages,
prevState.input],
input: ''
}
})
};
render() {
return (
<div>
<h2>Type in a new Message:</h2>
<input
value={this.state.input}
type={"text"}
onChange={this.handleChange}
/>
<button onClick={this.submitMessage} />
<ul>
{
this.state.messages.map(message => <li>{message}</li>)
}
</ul>
</div>
);
}
};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://learn.freecodecamp.org/front-end-libraries/react-and-redux/getting-started-with-react-redux