- For objects to be implicitly returned, wrap in paraentheses
const implicit = () => {
value: 1;
};
implicit(); // undefined
const implicit = () => ({ value: 1 });
implicit(); // { value: 1 }
- React throws a hissy fit if you use
.map()
without specifying a key property
- DO NOT DIRECTLY MODIFY STATE. ALWAYS MODIFY THROUGH THE SETTER FUNCTION
React Forms
- They seem to have some secret internal state that gets changed everytime it gets interacted with? Not sure how this is happening
- Note: controlled components are preferred since it ensures HTML rendered has parody with react state
- Always need:
- A name field that matches the formData (for controlled components)
- a onChange handler that changes the react state
- value/checked/whatever way that form element stores data (should be assigned to react state for controlled components)
- Other misc stuff (place holder, id, etc)
- Another note: buttons are type
submit
by default in react <form/>
elements
Infinite loops
useEffect can cause infinite loops when querying:
const component = () => {
const [starWarsData, setStarWarsData] = React.useState({});
fetch("https://swapi.dev/api/people/1")
.then((res) => res.json())
.then((data) => setStarWarsData(data));
return <p>{starWarsData}</p>;
};
- Component is called,
fetch
runs
setStarWarsData
is called in .then
- Component data is updated, component is called
- repeat
Can avoid this by using useEffect!