Skip to content

Instantly share code, notes, and snippets.

@diego-miranda-ng
Created September 17, 2020 01:35
Show Gist options
  • Save diego-miranda-ng/3fc32977e1a27bbdca62d93c6b1bd70b to your computer and use it in GitHub Desktop.
Save diego-miranda-ng/3fc32977e1a27bbdca62d93c6b1bd70b to your computer and use it in GitHub Desktop.
Exemplos de código para o artigo "React Hooks — Um 'Breve' Resumo"
// App.js
import React, { useState, useEffect } from "react";
import useWindowWidth from "./useWindowWidth";
export default App = (props) => {
const [isMobile, setIsMobile] = useState(false);
const width = useWindowWidth();
useEffect(() => {
setIsMobile(width <= 500);
}, [width]);
return <span>{isMobile ? "MOBILE" : "DESKTOP"}</span>;
};
import React from "react";
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0,
person: "TheAlfadur"
};
}
componentDidMount() {
console.log(this.state.count);
console.log(this.state.person);
}
componentDidUpdate() {
console.log(this.state.count);
console.log(this.state.person);
}
render() {
return (
<>
<button onClick={() => this.setState({ count: this.state.count + 1 })}>
INCREMENTAR O CONTADOR
</button>
<button
onClick={() =>
this.setState({
person:
this.state.person === "TheAlfadur" ? "Diego" : "TheAlfadur"
})
}
>
ALTERAR PESSOA
</button>
</>
);
}
}
import React from "react";
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
}
render() {
return (
<button onClick={() => this.setState({ count: this.state.count + 1 })}>
{this.state.count}
</button>
);
}
}
import React, { useState, useEffect } from "react";
export default App = (props) => {
const [count, setCount] = useState(0);
useEffect(() => {
console.log(count);
return () => {
console.log(`Componente desmontado. ${count}`);
};
});
return <button onClick={() => setCount(count + 1)}>SOMAR</button>;
};
import React, { useState, useEffect } from "react";
export default App = (props) => {
const [person, setPerson] = useState("TheAlfadur");
const [count, setCount] = useState(0);
useEffect(() => {
console.log(count);
}, [count]);
useEffect(() => {
console.log(person);
}, [person]);
return (
<>
<button onClick={() => setCount(count + 1)}>
INCREMENTAR O CONTADOR
</button>
<button
onClick={() =>
setPerson(person === "TheAlfadur" ? "Diego" : "TheAlfadur")
}
>
ALTERAR NOME
</button>
</>
);
};
import React, { useState } from "react";
export default App = (props) => {
const [count, setCount] = useState(0);
return <button onClick={() => setCount(count + 1)}>{count}</button>;
};
import React, { useState } from "react";
export default App = (props) => {
const p = { name: "Nome da pessoa", age: 24 };
const [person, setPerson] = useState(p);
return <span>{`${person.name} - ${person.age}`}</span>;
};
//useWindowWidth.js
import { useLayoutEffect, useState } from "react";
export default function useWindowSize() {
const [width, setWidth] = useState(0);
useLayoutEffect(() => {
const updateWidth = () => setWidth(window.innerWidth);
window.addEventListener("resize", updateWidth);
updateWidth();
return () => window.removeEventListener("resize", updateWidth);
}, []);
return width;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment