0

I'm trying to dynamically build a React element.

    let section = <span>Home> {header.section}</span>
    ...
    section += <span> > {location.title}</span>

    return (section)

If I don't try and add anything it works fine, when I do the above I get [object Object][object Object]

How do I go about building a React element dynamically?

2 Answers 2

2

you can try this

let section = [<span key={1}>Home> {header.section}</span>]
....
section.push(<span key={2}> {location.title}</span>)

return section

and in and in render

<div>{section}</div>
Sign up to request clarification or add additional context in comments.

Comments

0

You can declare section with empty array and keep pushing the elements you want to and then use map to render it :)

   ######### Process data here #########
   let section = [];
   section.push(<span>Home > Company Name</span>);
   section.push(<span>About us > Company Features</span>);

    ######### To render using map  #########
   const renderSection = section.map((val, idx) =>{
        return (val);
    })


    ######### To Display  #########
  {renderSection}

Hope this helps :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.