Blogs

Q1: What is the purpose of react-router?

Ans:React Router is a standard library for routing in React. It allows us to build a single-page web application with navigation without the page refreshing as the user navigates. React Router uses component structure to call components, which display the appropriate information. React Router offers loader route to load any data directly to the UI without using any useEffect hook. It also offers errorElement to handle route errors. New version of react router introduces different hooks to improve productivity.

Q2: How does context api work?

Ans: The React Context API is a way for a React app to effectively produce global variables that can be passed around. This is the alternative to "prop drilling" or moving props from grandparent to child to parent, and so on. Context is also considered as an easier, lighter approach to state management using Redux. To use context api First, we need to create the context, which we can later use to create providers and consumers. Provider is a component that as it's names suggests provides the state to its children. It will hold the "store" and be the parent of all the components that might need that store. Consumer as it so happens is a component that consumes and uses the state.

Q3: Explain useRef hook?

Ans: The useRef Hook allows you to persist values between renders. It can be used to store a mutable value that does not cause a re-render when updated. It can be used to access a DOM element directly. The useRef returns a mutable ref object. This object has a property called .current. The value is persisted in the refContainer.current property. These values are accessed from the current property of the returned object. The .current property could be initialised to the passed argument initialValue e.g. useRef(initialValue). The object can persist a value for a full lifetime of the component.