21
loading...
This website collects cookies to deliver better user experience
import React, { useState, useEffect } from 'react';
const Component = () => {
[notifications, setNotifications] = useState(0);
useEffect(() => {
document.title = `Oliver - {notifications} pending notifications`;
});
// ...
return (
// ...
);
};
import React, { useState, useEffect } from 'react';
const Component = () => {
[someState, setSomeState] = useState({});
useEffect(() => {
const subscription = subscribeToApi(() => {
// ...
setSomeState(...);
});
return () => {
subscription.unsubscribe();
};
});
// ...
return (
// ...
);
};
import React, { useState, useEffect } from 'react';
const Component = () => {
[someState, setSomeState] = useState({});
// This runs only once.
// Pay attention to the
// second argument '[]'.
useEffect(() => {
// ...
setSomeState(...);
}, []);
// ...
return (
// ...
);
};
import React, { useState, useEffect } from 'react';
const Component = () => {
[someState, setSomeState] = useState({});
// This runs each time
// someState changes
useEffect(() => {
// Could be an API call or whatever
validateSomeStateCorrectness(someState);
}, [someState]);
// ...
return (
// ...
);
};