javascriptreactjsreact-hooksreact-functional-componentreact-class-based-component

Converting class based component into functional based component and using react hooks


I am learning ReactJs and I am trying to implement below react classs based component into functional component but I am having difficulties in it. When I implement this into functional component it does not updates the webpage.

I have imported DISHES an object from where I gets the data. I am trying to set state using functional component

Below I have attached some code which I tried to use to set state

   class Main extends Component {
            constructor(props) {
                super(props);
                this.state = {
                    dishes: DISHES,
                    selectedDish: null,
                };
            }
        
            onDishSelect(dishId) {
                this.setState({ selectedDish: dishId });
            }
        
            render() {
                return (
                    <div>
                        <Navbar dark color="primary">
                            <NavbarBrand href="./" className="mx-5">
                                Ristorante De Confusion
                            </NavbarBrand>
                        </Navbar>
                        <Menu dishes={this.state.dishes} onClick={(dishId) => this.onDishSelect(dishId)} />
                        <Dishdetail
                            dish={this.state.dishes.filter((dish) => dish.id === this.state.selectedDish)[0]}
                        />
                    </div>
                );
            }
        }
        
        export default Main;
  

This is I am trying to convert

import React, { useState } from "react";
import { Navbar, NavbarBrand } from "reactstrap";
import Menu from "./Menu";
import Dishdetail from "./Dishdetail";
import { DISHES } from "../shared/dishes";

function Main() {
    const [dishes] = useState({ DISHES });
    const [selectedDish, updatedDish] = useState(null);

    function onDishSelect(dishId) {
        return updatedDish((selectedDish) => ({
            ...selectedDish,
            selectedDish: dishId,
        }));
    }

    return (
        <div>
            <Navbar dark color="primary">
                <NavbarBrand href="./" className="mx-5">
                    Ristorante De Confusion
                </NavbarBrand>
            </Navbar>
            <Menu dishes={dishes} onClick={(dishId) => onDishSelect(dishId)} />
            <Dishdetail dish={dishes.filter((dish) => dish.id === selectedDish)[0]} />
        </div>
    );
}

export default Main;

Solution

  • With what minimal e.g. you have given it would be as so, edit the props as required and used ...

    function Main(props) {
      const [state, setState] = useState({
        selectedDish: null,
        dishes: DISHES, // it's not clear from where it is referred `props.Dishes` if from props
      });
    
      function onDishSelect(dishId) {
        setState({ ...state, selectedDish: dishId });
      }
    
      return (
        <div>
          <Navbar dark color="primary">
            <NavbarBrand href="./" className="mx-5">
              Ristorante De Confusion
            </NavbarBrand>
          </Navbar>
          <Menu dishes={state.dishes} onClick={(dishId) => onDishSelect(dishId)} />
          <Dishdetail
            dish={state.dishes.filter((dish) => dish.id === state.selectedDish)[0]}
          />
        </div>
      );
    }
    
    export default Main;