Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
How to tackle with React.memo by passing props as object?

parent Component has 3 objects

const [inputErr, setInputErr] = useState({
  email: {
    err: false,
    errMsg: '',
  },
  password: {
    err: false,
    errMsg: '',
  },
});

const [inputValue, setInputValue] = useState({
  email: '',
  // password: "",
});

const inputFunction = {
  email: {
    input: {
      func: {
        onChange: testFunc,
      },
    },
  },
};

Parent component is rendering child in map function like this

{
  inputs.map((el, i) => {
    const rules = el.rules;
    const err = inputErr[el.name];
    const input = {
      value: inputValue[el.name],
    };
    return (
      <Input
        name={el.name}
        placeholder={el.placeholder}
        input={input}
        rules={rules}
        err={err}
        func={testFunc}
        key={el.name}
      />
    );
  });
}

Child Component is just like this

import React from 'react';
const Test = (data) => {
  console.log(data);
  console.log('hallo from Test ----------------- ');
  return <h1>Iam an test </h1>;
};

export default React.memo(Test);

Problem

React.memo not working as it should because of this piece of code as what I have observed

const input = {
  value: inputValue[el.name],
};

So can anybody help me to fix this bug

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

React.memo works as expected.
With this code

const input = {
    value: inputValue[el.name],
};

you create a new object every time and therefore the Child component is updated.
In order for it not to be updated, you can pass a custom comparison function to the React.memo

function areEqual(prevProps, nextProps) {
    /*
        return true if passing nextProps to render would return
        the same result as passing prevProps to render,
        otherwise return false
    */
    return prevProps.input.value === nextProps.input.value;
}

import React from 'react';
const Test = (data) => {
    console.log(data);
    console.log('hallo from Test ----------------- ');
    return <h1>Iam an test </h1>;
};

export default React.memo(Test, areEqual);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda