Skip to content

Instantly share code, notes, and snippets.

@JackHowa
Created July 3, 2018 01:47
Show Gist options
  • Save JackHowa/9fc9d6a35d5b1c0c3daf38cd9f244c63 to your computer and use it in GitHub Desktop.
Save JackHowa/9fc9d6a35d5b1c0c3daf38cd9f244c63 to your computer and use it in GitHub Desktop.
this is notes for the fcc course
@JackHowa
Copy link
Author

JackHowa commented Jul 3, 2018

https://learn.freecodecamp.org/front-end-libraries/react-and-redux/getting-started-with-react-redux

  • remember to initialize constructor and super with props
class DisplayMessages extends React.Component {
  // change code below this line
  constructor(props) {
    super(props);
    this.state = {input: '', messages: []}
  }
  // change code above this line
  render() {
    return <div />
  }
};

@JackHowa
Copy link
Author

JackHowa commented Jul 3, 2018

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>
    );
  }
};

@JackHowa
Copy link
Author

JackHowa commented Aug 1, 2018

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