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

272
Views
React: Passing value to a functional component results in undefined value

I'm trying to pass values one by one to a functional component from array.map function. Unfortunately the component is not redering the value.

enter image description here

This is what I'm getting. There are room names stored in DB that should be printed here.

Homescreen.js

import React, { useState, useEffect } from "react";
import axios from "axios";
import Room from "../components/Room";

export default function Homescreen() {
  const [rooms, setRooms] = useState([]);
  const [loading, setLoading] = useState();
  const [error, setError] = useState();

  useEffect(async () => {
    try {
      setLoading(true);
      const data = (await axios.get("/api/rooms/getallrooms")).data;
      setRooms(data);
      setLoading(false);
      setError(false);
    } catch (error) {
      setLoading(false);
      setError(true);
      console.log(error);
    }
  }, []);

  return (
    <div>
      {loading ? (
        <h1>Loading...</h1>
      ) : error ? (
        <h1>Error fetching details from API</h1>
      ) : (
        rooms.map((room) => {
          return (<div>
              <Room key={room._id} room={room}></Room>
          </div>
        )
        })
      )}
    </div>
  );
}

Room.js (Funcitonal component that should print room names):

import React from "react";

function Room(room){
    console.log(room.name)
    return(
        <div>
            <h1>Room name: {room.name}.</h1>
        </div>
    )
}

export default Room;

The data is fetched correctly from db because if, instead of passing the value to component I print directly into my main screen, the values are printed.

In otherwords, in Homescreen.js, doing <p>{room.name}</p> instead of <Room key={room._id} room={room}></Room> print room names correctly.

So I reckon the problem is coming when I'm passing the values as props.

Any help is much appreciated. Thanks.

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

0

The parameter passed to a function component is the props object which contains the passed props, so you just need to grab props.room from there:

function Room(props){
    console.log(props.room.name)
    return(
        <div>
            <h1>Room name: {props.room.name}.</h1>
        </div>
    )
}

Or, with object destructuring:

function Room({ room }){
    console.log(room.name)
    return(
        <div>
            <h1>Room name: {room.name}.</h1>
        </div>
    )
}
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!