React Components Flashcards
“In React, everything is a component.” Explain.
- Components are the building blocks of a React application’s UI.
- These components split up the entire UI into small independent and reusable pieces.
- React renders each of these components independent of each other without affecting the rest of the UI.
What is the purpose of render() in React.
- Each React component must have a render() mandatorily.
- Render returns a single React element which is the representation of the native DOM component. If more than one HTML element needs to be rendered, then they must be grouped together inside one enclosing tag such as , ,<div> etc. This function must be kept pure i.e., it must return the same result each time it is invoked.</div>
What is Props?
- Props is the shorthand for Properties in React.
- Props are read-only components which must be kept pure i.e. immutable.
- Props 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 helps in maintaining the unidirectional data flow and are generally used to render the dynamically generated data.
What is a state in React and how is it used?
- States are the source of data and must be kept as simple as possible.
- States are the objects which determine components rendering and behavior.
- States are mutable unlike the props and create dynamic and interactive components.
- States are accessed via this.state().
What is arrow function in React? How is it used?
- Shorter syntax for writing the function expression.
- These functions allow to bind the context of the components properly since in ES6 auto binding is not available by default.
- Arrow functions are mostly useful while working with the higher order functions.
What are the different phases of React component’s lifecycle?
- Initial Rendering Phase: This is the phase when the component is about to start its life journey and make its way to the DOM.
- Updating Phase: Once the component gets added to the DOM, it can potentially update and re-render only when a prop or state change occurs. That happens only in this phase.
- Unmounting Phase: This is the final phase of a component’s life cycle in which the component is destroyed and removed from the DOM.
What is an event in React?
In React, events are the triggered reactions to specific actions like mouse hover, mouse click, key press, etc. Handling these events are similar to handling events in DOM elements. But there are some syntactical differences like:
- Events are named using camel case instead of just using the lowercase.
- Events are passed as functions instead of strings.
The event argument contains a set of properties, which are specific to an event. Each event type contains its own properties and behavior which can be accessed via its event handler only.
What are synthetic events in React?
- Synthetic events are the objects which act as a cross-browser wrapper around the browser’s native event.
- They combine the behavior of different browsers into one API. This is done to make sure that the events show consistent properties across different browsers.
How do you modularize code in React?
- We can modularize code by using the export and import properties.
- They help in writing the components separately in different files.
How are forms created in React?
- React forms are similar to HTML forms.
- In React, the state is contained in the state property of the component and is only updated via setState().
- Elements can’t directly update their state and their submission is handled by a JavaScript function.
- This function has full access to the data that is entered by the user into a form.
What do you know about controlled and uncontrolled components?
Controlled Components
- They do not maintain their own state
- Data is controlled by the parent component
- They take in the current values through props and then notify the changes via callbacks
Uncontrolled Components
- They maintain their own state
- Data is controlled by the DOM
- Refs are used to get their current values