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

203
Vistas
React : Best practice to handle complex objects passed as props

Lets assume I have a React component (functional component) and i am passing multiple props. One of these props is a nested object like...

Example nested object

const user = {
    id: 101,
    email: 'jack@dev.com',
    personalInfo: {
        name: 'Jack',
        address: {
            line1: 'westwish st',
            line2: 'washmasher',
            city: 'wallas',
            state: 'WX'
        }
    }
}

For simplicity I want to pass the whole object, but instead of accessing it via user.personalInfo.adress.line1 I would like to save this to variable.

I am passing it to the following component...

Example React Component

const ExampleComponent = (props) => {
    
    // Example 1
    const [state1, setState1] = useState();

    useEffect(() => {
        setState1(user.personalInfo.adress.line1);
    })
    
    // Example 2
    const state1Var = user.personalInfo.adress.line1
}

I know that saving props to states is a bad practice. (= Example1) So whats the smartest way to do that? Saving it to a class variable? (= Example2)

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

0

You do not need a state for this. You can simply destructure the object on render of components.

Note: using useEffect without dependency variable, will retrigger the callback function on each render. It will be a memory issue useEffect(() => {},[]).

const user = {
  id: 101,
  email: "jack@dev.com",
  personalInfo: {
    name: "Jack",
    address: {
      line1: "westwish st",
      line2: "washmasher",
      city: "wallas",
      state: "WX",
    },
  },
};
const ExampleComponent = ({ user = {} }) => {
  const { personalInfo = {} } = user;
  const { name = "somedefault name", address = {} } = personalInfo;

  return (
    <div>
      {name}
      <br />
      {address.line1}
      <br />
      {address.line2}
    </div>
  );
};
about 4 years ago · Juan Pablo Isaza Denunciar

0

There's nothing wrong with passing complex objects as props. Why do you need to save it to state? Just destructure the object in the child component (assuming it is not possibly null or undefined). React functional components are just functions, you don't need to save something to state in order to use it. Since you're passing it as a prop from a parent component, presumably the parent component keeps track of its state; when the parent state updates, the prop will change and your child component will rerender (or, in other words, your child component's function will be called with the new prop value(s)).

const ExampleComponent = (props) => {
    const {personalInfo} = props.user;

    /* ... */
}
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