import React, { useState } from 'react';
function Example() {
// Declare a new state variable, which we'll call "count" const [count, setCount] = useState(0);
import React, { useState } from 'react';
function Example() {
// ...
}
import React, { useState } from 'react';
function Example() {
// Declare a new state variable, which we'll call "count" const [count, setCount] = useState(0);
function FriendStatusWithCounter(props) {
const [count, setCount] = useState(0);
useEffect(() => { document.title = `You clicked ${count} times`;
});
const [isOnline, setIsOnline] = useState(null);
useEffect(() => { function handleStatusChange(status) {
setIsOnline(status.isOnline);
}
ChatAPI.subscribeToFriendStatus(props.friend.id, handleStatusChange);
return () => {
ChatAPI.unsubscribeFromFriendStatus(props.friend.id, handleStatusChange);
};
});
// ...
}
import React, { useState } from 'react';
function Example() {
// Declare a new state variable, which we'll call "count" const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
class Example extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
}
render() {
return (
<div>
<p>You clicked {this.state.count} times</p>
<button onClick={() => this.setState({ count: this.state.count + 1 })}>
Click me
</button>
</div>
);
}
}
const Example = (props) => {
// You can use Hooks here!
return <div />;
}
class Example extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 }; }
function ExampleWithManyStates() {
// Declare multiple state variables!
const [age, setAge] = useState(42);
const [fruit, setFruit] = useState('banana');
const [todos, setTodos] = useState([{ text: 'Learn Hooks' }]);
<p>You clicked {this.state.count} times</p>
Recommend
React Using the State Hook Declaring a State Variable
React Using the State Hook What’s a Hook?
React Using the State Hook Hooks and Function Components
React Using the State Hook Equivalent Class Example
React Handling Events Passing Arguments to Event Handlers
React Rules of Hooks Explanation
React Rules of Hooks ESLint Plugin
ReactDOMServer Reference renderToStaticNodeStream()
ReactDOMServer Reference renderToNodeStream()
ReactDOMServer Reference renderToStaticMarkup()
ReactDOMServer Reference renderToString()
React Testing Recipes Multiple Renderers
React Testing Recipes Snapshot Testing
React Testing Recipes Mocking Modules
React Testing Recipes Data Fetching
React Testing Recipes Rendering
React Testing Recipes Setup/Teardown
React Rendering Elements Updating the Rendered Element
React Rendering Elements Rendering an Element into the DOM
React Component State What is the difference between passing an object or a function in setState?
React Component State Why is setState giving me the wrong value?
React Refs and the DOM Callback Refs
React Refs and the DOM Accessing Refs Refs and Function Components
React Refs and the DOM Accessing Refs Adding a Ref to a Class Component
React Refs and the DOM Accessing Refs Adding a Ref to a DOM Element
React Refs and the DOM Accessing Refs
React Refs and the DOM Creating Refs
File Structure Is there a recommended way to structure React projects? Grouping by file type
Glossary of React Terms Components props.children
Glossary of React Terms Components props
Glossary of React Terms Components
Glossary of React Terms Elements
React Code-Splitting Named Exports
React Code-Splitting Route-based code splitting
Code-Splitting React.lazy Error boundaries
React Code-Splitting Bundling Example
React Test Utilities Other Utilities Simulate
React Test Utilities Reference renderIntoDocument()
React Test Utilities Reference findRenderedComponentWithType()
React Test Utilities Reference scryRenderedComponentsWithType()
React Test Utilities Reference findRenderedDOMComponentWithTag()
React Test Utilities Reference scryRenderedDOMComponentsWithTag()
React Test Utilities Reference findRenderedDOMComponentWithClass()
React Test Utilities Reference scryRenderedDOMComponentsWithClass()
React Test Utilities Reference findAllInRenderedTree()
React Test Utilities Reference isCompositeComponentWithType()
React Test Utilities Reference isCompositeComponent()
React Test Utilities Reference isDOMComponent()
React Test Utilities Reference isElementOfType()
React Test Utilities Reference isElement()
React Test Utilities Reference mockComponent()
React Test Utilities Reference act()