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

255
Visualizações
Uncaught TypeError: Cannot assign to read only property 'amount' of object '#<Object>'

I'm fetching data from an API and then I save an array inside a state. While I'm trying to modify the specific field in object inside an array using input, I'm getting the following error: 'Uncaught TypeError: Cannot assign to read only property 'amount' of object '#''.

States (I'm using RTK Query):

const { data: Florists_data, refetch } = useGetFloristQuery(Number(sessionStorage.getItem('florist_id')));
const [flowersData, setFlowersData] = useState(Florists_data?.florist[0].flowers);
const [tmpFlowers, setTmpFlowers] = useState(Florists_data?.florist[0].flowers);

Update function:

const updateFieldChanged = (index: number, e: React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>) => {
        let newArr = [...tmpFlowers!];
        newArr[index].amount = Number(e.target.value); //GETTING ERROR HERE
        setTmpFlowers(newArr);
    }

Inputs:

{flowersData?.map((flower, index) => {
  return (
    <>
      <div className={classes.Nested_Flower_Container} key={index}>
        <div className={classes.Nested_Flower_Name}>
          {flower.name}
        </div>
        <div className={classes.Nested_Flower_Input} style={{ marginRight: '0.2em' }}>
          <TextField
            id="Amount"
            label="Amount"
            variant="outlined"
            size="small"
            type="number"
            onChange={(e) => {
              updateFieldChanged(index, e);
            }}
            className={classes_2.root}
            />
        </div>
      </div>
    </>)
})}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Spread operator normally creates a new object in memory, not just an address pointer. But when using it on an array of objects, it creates a second array like usual, but with all the objects inside being memory pointers. In short, what I believe is happening is the objects cant be changed because they're not copies. Try finding a deep clone code and testing of it works. (This is what's happening https://media.discordapp.net/attachments/335455578921107456/940745267324534825/MMAsMng1.png) which I also suspect is erroring out because you're modifying the state in an unapproved way

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