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

131
Visualizações
React useState being ignored

I have a small React app where you can click on a piece of text to replace it with an input field, to allow you to edit it.

The problem is that the EditableText component does not seem to want to display the editingText state in the input field.

See example here: https://codesandbox.io/s/distracted-ellis-kyrdb

For posterity, the code for the main app is:

export default function App() {
  const [cats, setCats] = useState([
    {
      id: 1,
      name: "fred"
    },
    {
      id: 2,
      name: "jim"
    }
  ]);
  const [activeCat, setActiveCat] = useState({});
  const handleClick = (cat, e) => {
    setActiveCat(cat);
  };
  const saveName = (newName) => {
    // save this cat's new name...
  };
  return (
    <div className="App">
      <div className="catMenu">
        {cats.map((row) => (
          <div
            className="catItem"
            key={row.id}
            onClick={(e) => handleClick(row, e)}
          >
            {row.name}
          </div>
        ))}
      </div>
      <div className="activeCat">
        <h2>
          <EditableText text={activeCat.name} saveText={saveName} />
        </h2>
      </div>
    </div>
  );
}

and for the EditableText component:

const EditableText = (props) => {
  const { text, saveText } = props;
  const [editing, setEditing] = useState(false);
  const [editingText, setEditingText] = useState(text);
  useEffect(() => {
    const handleKeydown = (e) => {
      if (editing) {
        if (e.keyCode === 13) {
          // Enter key pressed
          saveText(editingText);
          setEditing(false);
        }
        if (e.keyCode === 27) {
          // Escape key pressed
          setEditingText(text);
          setEditing(false);
        }
      }
    };
    window.addEventListener("keydown", handleKeydown);

    return () => {
      window.removeEventListener("keydown", handleKeydown);
    };
  }, [text, saveText, editing, editingText]);
  return (
    <div>
      <div
        onClick={() => setEditing(true)}
        className={`et-${editing ? "hidden" : "active"}`}
      >
        {text}
      </div>
      <input
        type="text"
        value={editingText}
        onChange={(e) => setEditingText(e.target.value)}
        className={`et-input-${editing ? "active" : "hidden"}`}
      />
    </div>
  );
};

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

0

Add another useEffect to listen to text prop change, you only use it as initial empty value:

useEffect(() => {
  setEditingText(text);
}, [text]);

Edit dreamy-shadow-vicxr

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