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

203
Visualizações
How to access values of state in other function

How can I use the value of state x in handleClick function to show an alert of input value in state x using Function component?

import React, { useState } from "react";
import "./App.css";

function About() {
  const [state, setState] = useState({
    x: "",
    output: [],
  });
  //for onChange Event
  const handleChnage = (e) => {
    setState({ ...state, [state.x]: e.target.value });
    console.log(e.target.value);
  };
  //for onClick Event
  const handleClick = () => {
    alert(state.x);
  };

  return (
    <>
      <h1>This is about about </h1>
      <input
        placeholder="Enter a number"
        value={setState.x}
        onChange={handleChnage}
      />
      <button onClick={handleClick}>get Table</button>
    </>
  );
}

export default About;
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

It should be x instead of state.x

const handleChnage = (e) => {
  setState({ ...state, x: e.target.value })
}

and the value should be state.x here instead of setState.x:

<input
  placeholder="Enter a number"
  value={state.x}
  onChange={handleChnage}
/>

Remember on hooks, the first parameter is the value, the second parameter is the setter function.

about 4 years ago · Juan Pablo Isaza Relatório

0

Two issues should be fixed:

  1. You do not need the computed value [state.x], it should be just x.
setState({ ...state, x: e.target.value });
  1. The value for the input should be state.x not setState.x
      <input
        placeholder="Enter a number"
        value={state.x}
        onChange={handleChnage}
      />

function About() {
  const [state, setState] = React.useState({
    x: "",
    output: []
  });
  //for onChange Event
  const handleChnage = (e) => {
    setState({ ...state, x: e.target.value });
    console.log(e.target.value);
  };
  //for onClick Event
  const handleClick = () => {
    alert(state.x);
  };

  return (
    <React.Fragment>
      <h1>This is about about </h1>
      <input
        placeholder="Enter a number"
        value={state.x}
        onChange={handleChnage}
      />
      <button onClick={handleClick}>get Table</button>
    </React.Fragment>
  );
}

ReactDOM.render(<About />, document.querySelector('.react'));
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div class='react'></div>

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