3

I've looked at a bunch of questions here and read the docs over and over, however this just doesn't seem to want to work no matter what I do.

This is supposed to return one thing if X is true and return something else if it's not. It's inside a map function because I need this to be done for multiple things at once.

    function ContentProcessing(props) {

        return (
            <div>
                props.content.map(content => {
                        {content.type === "card" ? (
                            <Card title={content.title} />
                        ) : (
                            <Content title={content.title} paragraph={content.guideline} />
                        )}
                })
            </div>
        );
    }

both <Card /> and <Content /> return one string

However I get the error
./src/App.js
Syntax error: /src/App.js: Unexpected token, expected , (79:13)

  77 |      <div>
  78 |          props.content.map(content => {
> 79 |                  {content.type === "card" ? (
     |                          ^
  80 |                      <Card title={content.title} />
  81 |                  ) ? (
  82 |                      <Content title={content.title} paragraph={content.guideline} />

I don't get why this isn't working.

2
  • You've got an extra open bracket. Commented Mar 20, 2018 at 15:42
  • 1
    Possible duplicate of ternary operator inside map Commented Mar 20, 2018 at 15:58

4 Answers 4

6

Issues:

1- Use {} to put expressions inside jsx (to put map inside div).

2- you are using {} means block body of arrow function, so you need to use return inside the function body, otherwise by default map returns undefined.

3- You are using {} twice, so 2nd {} will be treated as object and content.type will be treated as key and that key is not valid, thats why you are getting error.

4- Forgot to define the key on elements.

Use this:

return (
    <div>
        {
            props.content.map(content => content.type === "card" ? (
                    <Card title={content.title} />
                ) : (
                    <Content title={content.title} paragraph={content.guideline} />
                )
        )}
    </div>
);
Sign up to request clarification or add additional context in comments.

4 Comments

This doesn't give me an error, however nothing is being printed on the page
have you put same snippet inside function ContentProcessing(props) {....}, because it should work.
I have, now it's just giving me an error on the parenthesis that closes the map function
Nevermind, your answer is right, you just forgot to correct my mistake of using ? instead of : between statements. Thank you!
3

A couple of things are wrong I believe. You didn't add the curly braces in the first div. Inside the map you added two times the curly braces so you either remove one or add a return statement. You also added to "?" (the second one should be ":").

This should work:

function ContentProcessing(props) {
      return (
        <div>
          {props.content.map(content =>
            content.type === "card" ? <Card title={content.title} /> : <Content title={content.title} paragraph={content.guideline} /> 
          )}
        </div>
      );
    }

You can also add if else statements inside the map if you add braces:

function ContentProcessing(props) {
        return (
          <div>
            {props.content.map((content) => {
              if (content.type === "card") {
                return (<Card title={content.title} />);
              }
              return (<Content title={content.title} paragraph={content.guideline} />);
            })}
          </div>
        );
      }

Comments

1

Your syntax for the ternary operator is wrong. You have condition ? a ? b. The correct syntax is condition ? a : b.

Try

function ContentProcessing(props) {
        return (
            <div>
                {props.content.map(content => 
                        content.type === "card" ? (<Card title={content.title} />) : 
(<Content title={content.title} paragraph={content.guideline} />)
                )}
            </div>
        );
    }

3 Comments

This was a mistake, however the error I receive is still the same
Sorry your formatting was confusing without syntax highlighting. Try my latest edit
The error you were receiving is likely because you didn't escape the Javascript inside curly braces. You had the result of the map escaped within curly braces, but not the call itself. The entire JS block has to be within curly braces.
0

Multiple issues with the code.

return (
        <div>
            {props.content.map(content => 
                    content.type === "card" ? (
                        <Card title={content.title} />
                    ) : (
                        <Content title={content.title} paragraph={content.guideline} />
                    )
            )}
        </div>
    );

Extra brackets removed.

Conditional operator syntax was wrong.expression ? expression : expression

2 Comments

This just tells me content is undefined
sorry i missed a bracket.

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.