potrzebuję cheatsheet z reacta
- User
potrzebuję cheatsheet z reacta
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
potrzebuję cheatsheet z reacta
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Oto cheatsheet Reacta w języku polskim:
Komponent funkcyjny:
function Komponent() {
return <div>Witaj React!</div>;
}
Komponent klasowy:
class Komponent extends React.Component {
render() {
return <div>Witaj React!</div>;
}
}
Przekazanie props do komponentu:
<Komponent nazwa="Jan" wiek={25} />
Odbieranie props w komponencie:
function Komponent({ nazwa, wiek }) {
return <div>{nazwa} ma {wiek} lat.</div>;
}
Domyślne props (w klasach):
static defaultProps = {
nazwa: "Anonim",
};
useState (funkcyjne):
const [licznik, setLicznik] = useState(0);
setState (klasy):
this.setState((prevState) => ({
licznik: prevState.licznik + 1
}));
useState (zmienna stanu):
const [wartosc, setWartosc] = useState(wartość_inicjalna);
useEffect (efekty uboczne):
useEffect(() => {
// kod do wykonania po renderze
return () => {
// czyszczenie (np. unsubscribe)
};
}, [tablica_zależności]);
useContext (kontekst):
const tema = useContext(TemaContext);
useRef (odwołanie do elementu):
const inputRef = useRef();
useCallback (memoizacja funkcji):
const funkcja = useCallback(() => {}, [zależności]);
useMemo (memoizacja wartości):
const obliczone = useMemo(() => obliczenia(), [zależności]);
constructor() — inicjalizacja stanucomponentDidMount() — po renderzecomponentDidUpdate() — po aktualizacjicomponentWillUnmount() — przed usunięciemfunction klik() {
console.log("Kliknięto!");
}
return <button onClick={klik}>Kliknij</button>;
const [wartosc, setWartosc] = useState("");
return (
<input
value={wartosc}
onChange={(e) => setWartosc(e.target.value)}
/>
);
npm install react-router-domimport { BrowserRouter as Router, Route, Link } from "react-router-dom";
function App() {
return (
<Router>
<nav>
<Link to="/">Strona główna</Link>
</nav>
<Route path="/" exact>
<Home />
</Route>
</Router>
);
}
Stworzenie kontekstu:
const TemaContext = React.createContext();
Provider:
<TemaContext.Provider value="jasny">
<Komponent />
</TemaContext.Provider>
Akcja:
const akcja = { type: "AKCJA_TYP", payload: dane };
Reducer:
function reducer(state, action) {
switch (action.type) {
case "AKCJA_TYP":
return { ...state, ...action.payload };
default:
return state;
}
}
memo (zachowaj komponent, jeśli propsy nie zmieniły się):
const MemoizedKomponent = React.memo(Komponent);
klucz dla list:
{lista.map(item => (
<div key={item.id}>{item.nazwa}</div>
))}
this.state = ...).className.useEffect zamiast componentDidMount w funkcjach.Czy czegoś brakuje? 😊