React Research
React Research
React
Core Concepts
● Elements and JSX
● Components and Props
● Lists and Keys
● Events and Event Handlers
React Hooks
● State and useState
● Side Effects and useEffects
● Performance and useCallback
● Memoization and useMemo
● Refs and useRefs
Advanced Hooks
● Context and useContext
● Reducers and useReducer
● Writing custom hooks
● Rules of hooks
Core Concepts
function Layout(props) {
Return <div className="container">{props.children}</div>;
}
// the children props are very useful if we want the same component
to wrap all other components:
function IndexPage() {
return (
<Layout>
<Header />
<Hero />
<Footer />
</Layout>
);
}
// different page, but uses the same Layout component (the result of
using children prop)
function AboutPage() {
return (
<Layout>
<About />
<Footer />
</Layout>
);
}
function Header() {
return (
<nav>
<Logo />
Login */}
</nav>
);
}
● Fragments are special components for displaying multiple components without adding an
extra element to the DOM
○ Fragments are ideal for conditional logic
return (
<nav>
<Logo />
{/* we can render both components with a fragment
*/}
{/* fragments are concise: <> </> */}
{isAuthenticated? (
<>
<AuthLinks />
<Greeting />
</>
) : (
<Login />
)}
</nav>
);
}
Lists and Keys
function App() {
const people = ["John", "Bob", "Fred"];
// can interpolate returned list of elements in {}
return (
<ul>
{/* we're passing each array element as props */}
{people.map((person) => (
<Person name ={person}
))}
</ul>
);
}
return (
<ul>
{/* keys need to be primitive values, ideally a
generated id */}
{people.map((person) => (
<Person key={person} name={person} />
))}
</ul>
);
}
// if you don't have ids with your set of data or unique primitive
values,
// you can use the second parameter of .map() to get each element
index
function App() {
const people = ["John", "Bob", "Fred"];
return (
<ul>
{/* use array element index for key */}
{people.map((person, i) => (
<Person key{i} name{person} />
))}
</ul>
);
}
Events and Event Handlers
function handleToggleTheme() {
<button onclick="handleToggleTheme()">
Submit
</button>
{}
<button onClick={handleToggleTheme}>
Submit
</button>
● The most essential React events to know are onCLick and onChange
○ onClick handles click events on JSX elements (buttons)
○ onChange handles keyboard events (inputs)
function App() {
function handleChange(event) {
onChange
event.target
function handleSubmit() {
return (
<div>
onChange={handleChange} />
<button onClick={handleChange>Submit</button>
</div>
);
}
React Hooks
State and useState
● Note: Any hook in this section from the React package and can be imported individually
function App() {
const [language] = useState("javascript");
function App() {
return (
<div>
onClick={setterFn()} ? */}
Change language to JS
</button>
</div>
);
and not the App component re-renders to display the new state */
function App() {
return (
<div>
Change language to JS
</button>
<input
type="number"
value={yearsExperience}
onChange={(event) =>
setYearExperience(event.target.value)}
/>
<p>I am now learning{language}</p>
</div>
);
function handleChangeYearsExperience(event) {
const years = event.target.value;
// we need to pass in the previous state object with the spread
operator
setDeveloper({ ... developer, yearsExperience: years });
}
return (
<div>
{/* no need to get previous state here,
we are replacing the entire object */}
<button
onClick={() =>
setDeveloper({
language: "javascript",
yearsExperience: 0;
})
}
>
Change language to JS
</button>
{/* we can also pass a reference to the function */}
<input
type="number"
value={developer.yearExperience}
onChange={handleChangeYearsExperience}
/>
<p>I am now learning {developer.language}</p>
<p> I have {developer.yearsExperience} years of experience</p>
</div>
);
● If the new state depends on the previous state, to guarantee the update is done reliably, we
can use a function within the setter function that gives us the correct previous state
function App() {
const [developer, setDeveloper] = React.useState({
language: "".
yearsExperience: 0;
isEmployed: false,
});
function handleToggleEmployment(event) {
// we get the previous state variable's value in the
parameters
// we can name our 'prevState' however we like
setDeveloper((prevState) => {
return { ...prevState, isEmployed:
!prevState.isEmployed };
// it is essential to return the new state from this
function
});
}
return (
<button onClick={handleToggleEmployment}>Toggle Employment
Status</button>
);
}
// Pick a color from the colors array and make it the background
color
function App() {
const [colorIndex, setColorIndex] React.useState(0);
const colors = ["blue", "green", "red", "orange"];