Skip to main content

Command Palette

Search for a command to run...

Self review: Managing state in React

Updated
1 min read
Self review: Managing state in React
D

A passionate full-stack developer from @ePlus.DEV

  1. True or false? When lifting state up, you need to move the useState from a child component to a parent component.

    • True.

    • False.

  2. If the state variable holds an array or a string value, once you pass that state via props from a parent to a child, you can then read the length property from the received prop in the child component.

    • True

    • False

  3. What's wrong with this code?

     import React from "react";
     import Fruits from "./Fruits";
     import FruitsCounter from "./FruitsCounter";
    
     function App() {
       const [fruits] = useState([
           {fruitName: 'apple', id: 1},
           {fruitName: 'apple', id: 2},
           {fruitName: 'plum', id: 3},
       ]);
    
       return (
         <div className="App">
           <h1>Where should the state go?</h1>
           <Fruits fruits={fruits} />
           <FruitsCounter fruits={fruits} />
         </div>
       );
     }
    
     export default App;
    
    • If you don't add the setFruits state-updating function when descructuring values from useState, the app won't compile.

    • There's nothing wrong with the provided code.

    • The useState call should be React.useState.

React Basics

Part 7 of 18

In this module you will explore the basic structure and use of the React.js library. You will learn how to produce single page web applications using React components and to use JSX to style them.

Up next

Knowledge check: Passing state - React Basic

What is the Context API? An alternative way to working with state in React. A way to change the execution context of a function in JavaScript. When working with useState to keep state in a variable, you should not use array destructuring. True ...