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

161
Views
Error when clicking on an element: Objects are not valid as a React child

I use a map with custom markers on the site. On the right is a list of houses that match the markers. I need to implement the following: when clicking on a marker, the house that corresponds to the marker goes to 1st place in the list. Marker coordinates and house information comes from Firebase. Now I have implemented the code for this logic, but when I click on the marker, I get an error - Objects are not valid as a React child. How can it be solved?

const List = ({ selectedHouse }) => {
  const [houseTitles, setHouseTitle] = useState([]);
  useEffect(() => {
    const q = query(collection(db, "map-markers"));
    onSnapshot(q, (querySnapshot) => {
      setHouseTitle(
        querySnapshot.docs.map((doc) => ({
          id: doc.id,
          data: doc.data(),
        }))
      );
    });
  }, []);
  return (
    <div style={{ width: "50%" }}>
      {<ListItem title={houseTitles[selectedHouse]} />}
      {houseTitles
        .filter((title, index) => index !== selectedHouse)
        .map((title, index) => (
          <ListItem key={index} title={title.data.title} />
        ))}
    </div>
  );
};

console.log(houseTitles[selectedHouse]): enter image description here

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Try this:

const List = ({ selectedHouse }) => {
  const [houseTitles, setHouseTitle] = useState([]);

  useEffect(() => {
    const q = query(collection(db, "map-markers"));
    onSnapshot(q, (querySnapshot) => {
      setHouseTitle(
        querySnapshot.docs.map((doc) => ({
          id: doc.id,
          data: doc.data(),
        }))
      );
    });
  }, []);

  return (
    <div style={{ width: "50%" }}>
      <ListItem title={houseTitles[selectedHouse]?.data?.title} />
      {houseTitles
        ?.filter((title, index) => index !== selectedHouse)
        ?.map((title, index) => (
          <ListItem key={index} title={title?.data?.title} />
        ))}
    </div>
  );
};
about 4 years ago · Juan Pablo Isaza Report

0

You don't need curly brackets when using components like that;

...
...
return (
    <div style={{ width: "50%" }}>
      {houseTitles[selectedHouse]  && houseTitles[selectedHouse].data ? <ListItem title={houseTitles[selectedHouse].data.title} />:null }
      {houseTitles
        .filter((title, index) => index !== selectedHouse)
        .map((title, index) => (
          <ListItem key={index} title={title.data.title} />
        ))}
    </div>
  );
...
...

Also, you probably try to print title in ListItem component yet you pass the whole object for the selectedHouse

Additional info; selectedHouse is empty on the first render

about 4 years ago · Juan Pablo Isaza 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!