Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

238
Visualizações
React - passing props are viewed as object, error prompt 'objects are not valid as a react child'

Started learning React, and I'm trying to pass props to another component to render according to a list of values, but I get the noted error while trying so, even though I cannot find difference between my code and a working code. first component:

const navbarCollection = [
    { id: 1, item: 'Home', ref: '#'}, 
    { id: 2, item: 'Search', ref: '#'}, 
    { id: 3, item: 'Login', ref: '#'}, 
    { id: 4, item: 'Register', ref: '#'}, 
];

function UnorderedListNavBar() {
    return (
        <div id='wrapper-navBarList'>
            <ul className='navbar-nav mr-auto'>
                {
                    navbarCollection.map((menuItem) => {
                        return (<ListItem key={menuItem.id} mykey={menuItem.item} value={menuItem.ref} />);
                    })
                }
            </ul>
        </div>
    )
}

export default UnorderedListNavBar;

second component:

function ListItem(id, type, href) {
    return (
        <div id='wrapper-navBarListItem'>
            <li className='list-item active'>
                <a className='nav-link' href={href}>test</a>
            </li>
        </div>
    )
}

export default ListItem;

Honestly I tried everything. When I log what I'm sending with the first component to the second one, I get string as an outcome, yet when logging from the second component (or trying to use the data) it shows an object.

Please help me, I'll appreciate any help especially explanations so I can learn. Thanks a lot!

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

function ListItem({mykey, value}) {
    return (
        <div id='wrapper-navBarListItem'>
            <li className='list-item active'>
                <a className='nav-link' href={value}>test</a>
            </li>
        </div>
    )
}

export default ListItem;

The error you’re facing is because the object you’ve sent to child component is different than what you’re receiving in the child component

about 4 years ago · Juan Pablo Isaza Relatório

0

U need to destructure your props this way

function ListItem({ id, mykey, href }) {
  return (
    <div id="wrapper-navBarListItem">
      <li className="list-item active">
        <a className="nav-link" href={href}>
          test {mykey}
        </a>
      </li>
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza Relatório

0

//A more compact way to pass your props
const navbarCollection = [
    { id: 1, item: 'Home', ref: '#'}, 
    { id: 2, item: 'Search', ref: '#'}, 
    { id: 3, item: 'Login', ref: '#'}, 
    { id: 4, item: 'Register', ref: '#'}, 
];

function UnorderedListNavBar() {
    return (
        <div id='wrapper-navBarList'>
            <ul className='navbar-nav mr-auto'>
                {
                    navbarCollection.map((menuItem) => {
                        const {id, item, ref} = menuItem;
                        return <ListItem key={id} {...{id, item, href: ref}} />
                    })
                }
            </ul>
        </div>
    )
}

export default UnorderedListNavBar;

And ListItem component is,

function ListItem(props) {
    const {id, type, href} = props
    return (
        <div id='wrapper-navBarListItem'>
            <li className='list-item active'>
                <a className='nav-link' href={href}>test</a>
            </li>
        </div>
    )
}

export default ListItem;

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda