What is React Context, and how does it help in managing state and data sharing in a React application?
React Context - Move data without using props. This is great for anything that would normally be stored on local cache, such as location, dark mode, login authentication.
React Context helps to avoid props drilling.
createContext
method to create a context.Example
import React from 'react';
export const UserContext = React.createContext();
export default function App() {
return (
<UserContext.Provider value="Reed">
<User />
</UserContext.Provider>
)
}
function User() {
return (
<UserContext.Consumer>
{value => <h1>{value}</h1>}
{/* prints: Reed */}
</UserContext.Consumer>
)
}
Explain the useContext Hook and how it can be used to access data from a React Context within a functional component.
Another options is to use contextHook. This is a different way which can also access the useContext.
function User() {
const value = React.useContext(UserContext);
return <h1>{value}</h1>;
}
Redux is great if updating is needed, otherwise context is better.
Describe the purpose of Next.js, and provide an example from the Vercel Next.js Examples reading on how it can be used to build a scalable web application.
https://github.com/vercel/next.js/tree/canary/examples/headers
Honestly, I don’t know what the point is. More of the same if you ask me. I understand it allows for faster rendering due to server-side rendering, but with modern hardware, I don’t see this as being an issue. Perhaps the purpose is data control?
More about trade-offs between redux, context, props, etc.