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

195
Visualizações
usestate variable shows undefined value?

I'm getting an undefined error on value variable which is a state. when logging the value in the console, It initially takes the value from props.value inside the useEffect. But then in concurrent renders it's written undefined so I'm guessing value variable becomes undefined after that. But this issue is not there when removing the value.length code. then the undefined error doesn't happen and the props.value is inside the textarea. But I need the value.length code to determine some CSS classes. Hence I'm guessing before the value variable is set by props.value, value.length code runs. But then the state default is set as "" so value.length` should return 0 even if it runs first right? So I'm not understanding why my state variable value is becoming undefined in between.

import React, { useState, useEffect } from "react";

const TextArea = (props) => {
  const [value, setValue] = useState("");

  useEffect(() => {
    window.sessionStorage.setItem(props.name, value);
  }, [value, props]);

  useEffect(() => {
    setValue(props.value);
  }, [props.value]);

  return (
    <div className="input-box input-box--3">
      <textarea
        required
        name={props.name}
        type="text"
        rows={props.rows}
        value={value}
        onChange={(event) => setValue(event.target.value)}
        className={
          console.log(value)
          // value.length === 0
          //   ? "input-box--field input-box--field--3"
          //   : "input-box--field input-box--field--3 input-box--field--3--not-empty"
        }
        autoComplete={props.autoComplete}
      />
      <span
        className={
          value.length === 0
            ? "input-box--placeholder"
            : "input-box--placeholder input-box--placeholder--not-empty"
        }
      >
        {props.placeholder}
      </span>
    </div>
  );
};

export default TextArea;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The value depends on the value prop, so if you'll pass something in this prop, it should work:

https://codesandbox.io/s/mystifying-cartwright-x6ldj0?file=/src/App.js

However, you can make it more bulletproof by checking if the passed prop is undefined:

  setValue(props.value ? props.value : "");
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