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

252
Vistas
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 Respuestas
Responde la pregunta

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 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