0

I have a component which is going to rely on the results of an array.map to determine it's output. In this case the array contains an element called "name", and one of the names is 'Welcome.' What needs to happen is the component spits out a particular div for the 'Welcome' instance and different content (an Accordion component) for every other instance. I've used a ternary operator in the render which I'm then calling in the return, but it's outputting the same text for every instance of the component (the text specified for only the 'Welcome' instance.) I can't for the life of me figure out what I'm doing wrong. Here's the code:

export default class Back extends React.Component {

constructor(props) {
 super(props)
}

render() {

 const CheckDomains = this.props.totaldomains.map((domain, index) => {    
    <div>
    {
      domain.name === 'Welcome' ?
        <div>This is welcome text.</div>
      :
        <div>This is accordion text.</div>
    }
    </div>
   )
 });


 return(
      <div className='back'>
        {CheckDomains}
      </div>
     );
   }
 }
1

1 Answer 1

1

You need to return something in your map callback function.

Like so:

const CheckDomains = this.props.totaldomains.map((domain, index) => {    
  return  (
      <div>
          {
            domain.name === 'Welcome' ?
              <div>This is welcome text.</div>
            :
              <div>This is accordion text.</div>
          }
        </div>
    )
});
Sign up to request clarification or add additional context in comments.

4 Comments

That solution is putting outputting both divs on top of each other on every instance of the component.
Can I see the JSON of your this.props.totaldomains?
@CHays412 I have tried to replicate it in this sandbox env and it is working - codesandbox.io/s/z3jp1lxv9p
awesome I will check this out! Thank you so much.

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.