View on GitHub

marymafa.github.io


layout: post title: API date: 2018-03-14 —

API

getInitialState

Implement the function getInitialState, which returns… the initial state of the component. This is an object map of keys to value.

this.state

To access a component’s state, use this.state, just like how we use this.props.

this.setState

To update a component’s state, call this.setState with an object map of keys to updated values. Keys that are not provided are not affected. When a component’s state changes, render is called with the new state and the UI is updated to the new output. This is the heart of React.

The new context API consists of three main parts:

  1. React.createContext which is passed the initial value (and optionally a fancy opt-out function that uses a bitmask). This returns an object with a Provider and a Consumer.

  2. The Provider component is used higher in the tree and accepts a prop called value (which can be anything).

  3. The Consumer component is used anywhere below the provider in the tree and accepts a prop called “children” which must be a function that accepts the value and must return a react element (JSX).