Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
elements to the array do not return the current state

For example, I add 10 components.

When I click the show logs button on the 5th line, the first 4 components appear on the console.

But even if I click the button on the 5th line, I want all the components to appear on the console.

import React, { useState, useEffect } from "react";
import ReactDOM from "react-dom";
import { observer } from "mobx-react-lite";

function App() {
  const [component, setComponent] = useState([]);

  useEffect(() => {});

  const Test = observer(() => {
    return (
      <div>
        <p>
          Test <button onClick={testFunction("myFunction"))}>Show logs</button>
        </p>
      </div>
    );
  });

  function testFunction(a) {
    console.log(component);
    console.log(a)
  }

  return (
    <div>
      {component.map((Input, index) => (
        <Input key={index} />
      ))}
      <button onClick={() => setComponent([...component, Test])}>
        Click me
      </button>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

codesandbox: https://codesandbox.io/s/react-hooks-useeffect-forked-7q15no

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

It seems like the Observer is getting a previous version of component, which is not consistent with the updated component. In cases like this, useEffect is usually a better option as it updates instantly instead of asynchronously. Refer to the code below which is working:

The updater simply makes the useEffect hook run which logs the latest value of component. The onClick now simply updates the updater.

import React, { useState, useEffect } from "react";
import ReactDOM from "react-dom";
import { observer } from "mobx-react-lite";

function App() {
  const [component, setComponent] = useState([]);
  const [updater, setUpdater] = useState(0);

  useEffect(() => {console.log(component)}, [updater]);

  const Test = observer(() => {
    return (
      <div>
        <p>
          Test <button onClick={() => setUpdater(updater + 1)}>Show logs</button>
        </p>
      </div>
    );
  });

  return (
    <div>
      {component.map((Input, index) => (
        <Input key={index} />
      ))}
      <button onClick={() => setComponent([...component, Test])}>
        Click me
      </button>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!