Skip to content

Instantly share code, notes, and snippets.

View izabellewilding's full-sized avatar

Izabelle Wilding izabellewilding

View GitHub Profile
export default class ExampleComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
name: "John"
};
this.changeName = this.changeName.bind(this);
}
changeName() {
componentDidMount() {
fetch("https://example.api.com")
.then(response => response.json())
.then(data => this.setState({ data }));
}
const [data, setData] = useState()
useEffect(() =>
fetch('https://example.api.com')
.then(response => response.json())
.then(data => this.setState({data});
), []);
componentDidUpdate(prevProps) {
if (this.props.userName !== prevProps.userName) {
this.fetchData(this.props.userName);
}
}
useEffect(() => {
const handleScroll = () => {
console.log("page was scrolled";
};
window.addEventListener("scroll", handleScroll);
return () => {
window.removeEventListener("scroll", handleScroll);
}
}, []);
class ErrorBoundary extends React.Component {
constructor(props) {
super(props)
this.state = { error: null }
}
componentDidCatch(error) {
this.setState({
error,
})
<ErrorBoundary>
<ChildComponent>
</ErrorBoundary>
import "./styles.css";
import { useCallback, useEffect, useState } from "react";
const SubComponent = ({ handleClick, isRed }) => {
useEffect(() => {
const interval = setInterval(() => {
handleClick(!isRed);
}, 1000);
return () => clearInterval(interval);