React APIs
The React package contains all the APIs necessary to define and use components.
Installation
It is available as react on npm. You can also add React to the page as a <script> tag.
Terminal
npm install react
// Importing a specific API:
import { useState } from 'react';
// Importing all APIs together:
import * as React from 'react';If you use React on the web, you’ll also need the same version of ReactDOM.
Exports
State
useState
Declares a state variable.
function MyComponent() {
const [age, setAge] = useState(42);
// ...useReducer
Declares a state variable managed with a reducer.
function MyComponent() {
const [state, dispatch] = useReducer(reducer, { age: 42 });
// ...Context
useContext
Reads and subscribes to a context.
function MyComponent() {
const theme = useContext(ThemeContext);
// ...createContext
Creates a context that components can provide or read.
const ThemeContext = createContext('light');Refs
useRef
Declares a ref.
function MyComponent() {
const inputRef = useRef(null);
// ...This section is incomplete and is still being written!