Jump to navigation

Stateful/Stateless components

Stateful component

Handles and manipulates state.

class Quote extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      liked: false
    };
  }

  handleLikes() {
    this.setState({
      liked: !this.state.liked
    }
  }

  render() {
    return (
      <div>
        <LikeCounter
          liked={this.state.liked}
          handleLikes={this.handleLikes.bind(this)}
        />
      </div>
    );
  }

Stateless component

Stateless components don't contain any state, they simply render the information, in this case the prop username

export default class Name extends Component {
  render() {
    return (
      <div>
        <h1>{this.props.username}</h1>
      </div>
    );
  };
};

<name username={this.state.username} />