Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

136
Vistas
Get value returned by useState

Get value returned by useState when state updates first time.

In following code : Initial value is 0 and after clicking on button value will be 1.I want to store value1 in firstValue and value2 in secondValue and these values must be immutable.

function App() {
  const [count, setCount] = React.useState(0);  
// const firstValue = 
// const secondValue =

  return <button onClick={() => setCount(count+1)}> value {count}</button>;
}

ReactDOM.render(<App count={0}/>, document.getElementById('root'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.6/umd/react.production.min.js" integrity="sha256-3vo65ZXn5pfsCfGM5H55X+SmwJHBlyNHPwRmWAPgJnM=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.6/umd/react-dom.production.min.js" integrity="sha256-qVsF1ftL3vUq8RFOLwPnKimXOLo72xguDliIxeffHRc=" crossorigin="anonymous"></script>

<div id='root'></div>

Value will be stored only first and second click of button and these values must be immutable.

Expected Output

firstValue = value1
secondValue = value2
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Perhaps another state variable can be responsible for this part?

const MAX = 2
function App() {
  const [count, setCount] = React.useState(0);
  const [values, setValues] = React.useState([]);
  function onClick () {
    if (count < MAX) {
      setValues([...values, count])
    }
    setCount(count+1)
  }
  
  const [firstValue, secondValue] = values

  return (
    <div>
      <button onClick={onClick}> value {count}</button>
      <p>first value: {firstValue}</p>
      <p>second value: {secondValue}</p>
    </div>
  )
}

ReactDOM.render(<App count={0}/>, document.getElementById('root'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.6/umd/react.production.min.js" integrity="sha256-3vo65ZXn5pfsCfGM5H55X+SmwJHBlyNHPwRmWAPgJnM=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.6/umd/react-dom.production.min.js" integrity="sha256-qVsF1ftL3vUq8RFOLwPnKimXOLo72xguDliIxeffHRc=" crossorigin="anonymous"></script>

<div id='root'></div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Assuming value1 is count's previous state, and value2 is count's current state.

function App() {
  const [count, setCount] = React.useState(0);
  //Initialize the variables
  let firstValue = count;
  let secondValue = count;

  return (
    <button
      onClick={() => {
        firstValue = secondValue; // set first value equal to previous count value
        secondValue = count + 1; // set second value equal to new count value
        setCount(count + 1); // update the state
      }}
    >
      value {count}
    </button>
  );
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

  • Not sure this is optimal solution, but a way using useRef
  • gather's all the values not only first two, but yes key names can be altered as per your convenience

    function App() {
      const [count, setCount] = React.useState(0);  
      const values = React.useRef({});
  
      const updateValue = () => {
        setCount(count+1);
        // get previous values using spread, and then add the dynamic using square brackets
        values.current = {...values.current, ["value" + (count+1)]: count+1 }
      }
      // you may require this values.current
      console.log(values.current)
      return <button onClick={() => updateValue() }> value {count}</button>;
    }

    ReactDOM.render(<App count={0}/>, document.getElementById('root'))
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.6/umd/react.production.min.js" integrity="sha256-3vo65ZXn5pfsCfGM5H55X+SmwJHBlyNHPwRmWAPgJnM=" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.6/umd/react-dom.production.min.js" integrity="sha256-qVsF1ftL3vUq8RFOLwPnKimXOLo72xguDliIxeffHRc=" crossorigin="anonymous"></script>

    <div id='root'></div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda