0

My goal is to render a component based on the type prop I am passing to the component.

Here is how I am passing the type prop to the component:

export default function SearchListItem({
  type,
}) {

And here is my switch case statement:

const notificationComponent = (type) => {
    switch (type) {
      case type == 1:
        return <Text>1</Text>;
      case type == 2:
        return <Text>2</Text>;
      case type === 3:
        return <Text>3</Text>;
      case type === 4:
        return <Text>3</Text>;

      default:
        return <Text>No project</Text>;
    }
  };

And here is how I am rendering the component on the page:

  <Title>{notificationComponent(type)}</Title>

Here is how I am passing the props in from the parent component:

<ActivityListItem type={3} />
<ActivityListItem type={4} />

Currently, the component is only returning the No project case although I am passing in props that should match the different cases. Any idea what I'm doing wrong?

4
  • @jnpdx Just updated the code Commented May 10, 2021 at 22:42
  • Looks like you need to call notificationComponent(type)? You're calling it with no arguments right now. Commented May 10, 2021 at 22:43
  • @jnpdx You asked how I am passing in the props Commented May 10, 2021 at 22:45
  • @maazadeeb Hmmm, still doesn't work Commented May 10, 2021 at 22:45

2 Answers 2

2

Your case statements are wrong. Only specify the value, not a comparison.

switch (type) {
    case type == 1:
        return <Text>1</Text>;
    ...
}

wouldn't do anything, as type == 1 would get evaluated to true and type is not equal to true.

Instead, do:

switch (type) {
    case 1:
        return <Text>1</Text>;
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

It was luck, though. Next time post a minimal reproducable example, to eliminate the luck factor and reduce the frustration of the people trying to help.
0

This may well mean that your var type is not defined or passed correctly.

You may want to console.log this var and see what it really is.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.