0

I am trying to render a React component that outputs content from an API endpoint.

I am successfully able to display the content, but now I want to add some computation to what is being displayed.

In this example, I want the Badge to be green success badge if the current object has a status of "Enabled", and I want the Badge to be a yellow warning badge if the current object has a status of "Paused".

It does not appear that I can simply use JavaScript here. Any ideas?

Thanks!

    const renderInformation = data.map((item) => (
    <Card
      key={item.id}
      title={item.name}
      actions={[{ content: "Details" }]}
    >
      <Card.Section>
        <Stack>
          if (item.status == 'enabled') { // <---- THIS IS WHERE I WANT TO USE AN IF STATEMENT
            <Badge status="success">Enabled</Badge>
          }
          if (item.status == 'paused') { // <---- THIS IS WHERE I WANT TO USE AN IF STATEMENT
            <Badge status="attention">Paused</Badge>
          }
        </Stack>
      </Card.Section>
    </Card>
  ));

2 Answers 2

1

You can use js with jsx, just add a {} around your js code. And better use expression than statement. so you code will look like below:

const renderInformation = data.map((item) => (
  <Card
    key={item.id}
    title={item.name}
    actions={[{ content: "Details" }]}
  >
    <Card.Section>
      <Stack>
        {item.status == 'enabled'&& <Badge status="success">Enabled</Badge>}
        {item.status == 'paused' && <Badge status="attention">Paused</Badge>}
      </Stack>
    </Card.Section>
  </Card>
));
Sign up to request clarification or add additional context in comments.

Comments

1
   const renderInformation = data.map((item) => (
    <Card
      key={item.id}
      title={item.name}
      actions={[{ content: "Details" }]}
    >
      <Card.Section>
        <Stack>
          {item.status == 'enabled' && <Badge status="success">Enabled</Badge> }
          {item.status == 'paused' && <Badge status="attention">Paused</Badge> }
        </Stack>
      </Card.Section>
    </Card>
  ));

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.