Scenario Flashcards
You’re building a shopping cart feature in an e-commerce app using Redux. How would you add an item to the cart and update the cart state?
To add an item to the cart and update the cart state, you would dispatch an action with the item details. The reducer would handle updating the cart state by appending the new item to the existing cart items array.
In a Redux-based application, how can you access the current state of the application?
You can access the current state of the application using the useSelector hook provided by the react-redux library. This hook allows you to select specific parts of the state based on your component’s needs.
You’re creating a counter component using setState in React. How would you increment the counter when a button is clicked?
You would define a state variable for the counter, say count, using useState. Then, in the button’s click handler, you would call setCount(prevCount => prevCount + 1) to increment the counter.
You’re building a form in a React component, and you want to store and manage the form data using setState. How would you handle changes in form inputs and update the state accordingly?
For each input element, you would set its value attribute to the corresponding state variable and handle its onChange event by calling a function that updates the state using setState with the new value.