How do uncontrolled components in React handle form data?
Explanation:
Uncontrolled components in React don't rely on the component's state for form data. Instead, they use refs to directly interact with the DOM, allowing you to extract values when needed.
What will happen if you update the state directly in a class component instead of using this.setState()
?
Explanation:
Directly updating this.state
will modify the state, but React won't be aware of this change. this.setState()
is crucial as it notifies React about the state update, triggering the necessary re-rendering process.
Why is it generally not recommended to mutate the DOM directly in React?
Explanation:
React uses a virtual DOM to efficiently update the UI. Directly manipulating the DOM can interfere with React's reconciliation process and create synchronization issues between the virtual and actual DOM.
In JSX, how would you set the 'class' attribute for an element?
Explanation:
Since 'class' is a reserved keyword in JavaScript, JSX uses 'className' to set the CSS class of an element.
What is the purpose of event.preventDefault()
in React event handling?
Explanation:
Similar to vanilla JavaScript, event.preventDefault()
is used in React to prevent the browser's default behavior associated with an event, such as a link redirect or form submission.
What are Synthetic Events in React?
Explanation:
Synthetic events are plain JavaScript objects created by React that hold information about an event across different browsers. They provide a consistent API and prevent cross-browser inconsistencies.
What is the primary purpose of components in React?
Explanation:
Components are the building blocks of a React application. They encapsulate UI elements, making them reusable and easier to manage.
How can you display validation errors to the user in a React form?
Explanation:
A common approach is to conditionally render error messages in your component's JSX. After validating input, you can update the component's state with error information and use it to control the display of error messages.
What is the correct syntax for defining default props in a functional component in React?
Explanation:
This syntax correctly assigns an object with default prop values to the 'defaultProps' property of the 'MyComponent' function, providing fallback values for props if not provided.
What is the purpose of the useState
hook in React?
Explanation:
The useState
hook is the primary way to introduce and manage state within functional components in React. It provides a way to store and update values that can cause re-renders when changed, making the component dynamic.