React JS 15 Interview Qns
React JS 15 Interview Qns
Ans: Browsers can only read JavaScript objects but JSX in not a regular JavaScript object. Thus,
to enable a browser to read JSX, first, we need to transform JSX file into a JavaScript object using
JSX transformers like Babel and then pass it to the browser.
Ans: Props is the shorthand for Properties in React. They are read-only components which must
be kept pure immutable. They are always passed down from the parent to the child components
throughout the application. A child component can never send a prop back to the parent
component. This help in maintaining the unidirectional data flow and are generally used to render
the dynamically generated data.
5. What is useState() in React?
Ans: The useState() is a built-in React Hook that allows you for having state variables in
functional components. It should be used when the DOM has something that is dynamically
manipulating/controlling.
In the below-given example code, The useState(0) will return a tuple where the count is the first
parameter that represents the counter’s current state and the second parameter setCounter
method will allow us to update the state of the counter.
Ans: React Hooks are the built-in functions that permit developers for using the state and
lifecycle methods within React components. These are newly added features made available in
React 16.8 version. Each lifecycle of a component is having 3 phases which include mount,
unmount, and update. Along with that, components have properties and states. Hooks will allow
using these methods by developers for improving the reuse of code with higher flexibility
navigating the component tree.
Using Hook, all features of React can be used without writing class components. For example,
before React version 16.8, it required a class component for managing the state of a component.
But now using the useState hook, we can keep the state in a functional component.
Ans: React Router is a routing library which allows you to add new screen flows to your
application, and it also keeps URL in sync with what’s being shown on the page.
Ans: Keys allow you to provide each list element with a stable identity. The keys should be
unique.
Ans: Children props are used to pass component to other components as properties. You can
access it by using
{props.children}
Ans: React context helps you to pass data using the tree of react components. It helps you to
share data globally between various react components.
Ans: Differences between the conventional routing and the routing in React can be shown using
the following aspects:
• Pages: Each view is considered as a new file in conventional routing while it is considered as
a single HTML entity in React.
• Navigation: In conventional routing, users have to move across web pages for viewing. In
React, the views are not refreshed as objects are re-issued to create new views.
Ans. Keys are used in React to identify unique VDOM Elements with their corresponding data
driving the UI and help React identify which items have changed, are added, or are removed.
Keys should be a unique number or string.
Ans: ‘Refs’ is short for references in React. Refs are used to store a reference to a single React
element or a React component. This is later returned using the render function.
15. How can you tell React to build in the production mode?
Ans: React can be coded to directly build into production by setting the process.env.NODE_ENV
variable to production. When React is in production, warnings and other development features
are not shown.