Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

132
Vistas
Convert the code in function component to class component

I am having this react js code and I am trying to convert the following code into class component

I don't know how to do that if someone can create a working sandbox then it will be helpful

import React, { useEffect, useState } from "react";
import "./styles.css";

const data = [
  {
    id: "1",
    name: "Jane",
    lastName: "Doe",
    age: "25"
  },
  {
    id: "2",
    name: "James",
    lastName: "Doe",
    age: "40"
  },
  {
    id: "3",
    name: "Alexa",
    lastName: "Doe",
    age: "27"
  },
  {
    id: "4",
    name: "Jane",
    lastName: "Brown",
    age: "40"
  }
];

export default function App() {
  const [peopleInfo, setPeopleInfo] = useState({});

  const toggleHandler = (item) => () => {
    setPeopleInfo((state) => ({
      ...state,
      [item.id]: state[item.id]
        ? null
        : {
            id: item.id,
            first: item.name,
            last: item.lastName,
            age: item.age
          }
    }));
  };

  useEffect(() => {
    console.log(peopleInfo);
  }, [peopleInfo]);

  return (
    <div className="App">
      <table>
        <tr>
          {data.map((item) => {
            return (
              <div
                key={item.id}
                style={{
                  display: "flex",
                  width: "150px"
                }}
              >
                <input
                  onChange={toggleHandler(item)}
                  checked={peopleInfo[item.id]}
                  style={{ margin: "20px" }}
                  type="checkbox"
                />
                <td style={{ margin: "20px" }}>{item.name}</td>
                <td style={{ margin: "20px" }}>{item.lastName}</td>
                <td style={{ margin: "20px" }}>{item.age}</td>
              </div>
            );
          })}
        </tr>
      </table>
    </div>
  );
}

I am having this react js code and I am trying to convert the following code into a class component

I don't know how to do that if someone can create a working sandbox then it will be helpful I am having this react js code and I am trying to convert the following code into a class component

I don't know how to do that if someone can create a working sandbox then it will be helpful

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

  1. The componentDidUpdate life cycle method corresponds to useEffect hook.

  2. The render method should return the JSX to be rendered from the class component.

  3. this.setState method is available by extending the Component class (from prototype chaining).

This will be the corresponding class component.

import React, { Component } from "react";

const data = [
  {
    id: "1",
    name: "Jane",
    lastName: "Doe",
    age: "25"
  },
  {
    id: "2",
    name: "James",
    lastName: "Doe",
    age: "40"
  },
  {
    id: "3",
    name: "Alexa",
    lastName: "Doe",
    age: "27"
  },
  {
    id: "4",
    name: "Jane",
    lastName: "Brown",
    age: "40"
  }
];

export default class App extends Component {
  state = {};

  toggleHandler = (item) => () => {
    this.setState((state) => ({
      ...state,
      [item.id]: state[item.id]
        ? null
        : {
            id: item.id,
            first: item.name,
            last: item.lastName,
            age: item.age
          }
    }));
  };

  componentDidUpdate() {
    console.log(this.state);
  }

  render() {
    return (
      <div className="App">
        <table>
          <tr>
            {data.map((item) => {
              return (
                <div
                  key={item.id}
                  style={{
                    display: "flex",
                    width: "150px"
                  }}
                >
                  <input
                    onChange={this.toggleHandler(item)}
                    checked={this.state[item.id]}
                    style={{ margin: "20px" }}
                    type="checkbox"
                  />
                  <td style={{ margin: "20px" }}>{item.name}</td>
                  <td style={{ margin: "20px" }}>{item.lastName}</td>
                  <td style={{ margin: "20px" }}>{item.age}</td>
                </div>
              );
            })}
          </tr>
        </table>
      </div>
    );
  }
}

Edit practical-keller-nby4s0

about 4 years ago · Juan Pablo Isaza Denunciar

0

ComponentDidUpdate will be called when each it rerender with state update.

import "./styles.css";
import React from "react";

const data = [
  {
    id: "1",
    name: "Jane",
    lastName: "Doe",
    age: "25"
  },
  {
    id: "2",
    name: "James",
    lastName: "Doe",
    age: "40"
  },
  {
    id: "3",
    name: "Alexa",
    lastName: "Doe",
    age: "27"
  },
  {
    id: "4",
    name: "Jane",
    lastName: "Brown",
    age: "40"
  }
];

export default class App extends React.Component {
  state = {
    peopleInfo: {}
  };

  toggleHandler(item) {
    this.setState({
      peopleInfo: {
        ...this.state.peopleInfo,
        [item.id]: this.state.peopleInfo[item.id]
          ? null
          : {
              id: item.id,
              first: item.name,
              last: item.lastName,
              age: item.age
            }
      }
    });
  }

  componentDidUpdate() {
    console.log(this.state.peopleInfo);
  }
  render() {
    return (
      <div className="App">
        <table>
          <tr>
            {data.map((item) => {
              return (
                <div
                  key={item.id}
                  style={{
                    display: "flex",
                    width: "150px"
                  }}
                >
                  <input
                    onChange={() => this.toggleHandler(item)}
                    checked={this.state.peopleInfo[item.id]}
                    style={{ margin: "20px" }}
                    type="checkbox"
                  />
                  <td style={{ margin: "20px" }}>{item.name}</td>
                  <td style={{ margin: "20px" }}>{item.lastName}</td>
                  <td style={{ margin: "20px" }}>{item.age}</td>
                </div>
              );
            })}
          </tr>
        </table>
      </div>
    );
  }
}

CodeSandBox

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda