1

Hi I'm a beginner in react, I was getting an error for scenarios when no props get passed to my component, it said something like "you can't do a map of undefined" so I added this if statement. But now I get a parsing error on line 4. Not sure' what's wrong with my code:

import React from 'react';

export const Record = props => (
  if (props) {
    <div className='card-container'>
      {props.rows.drivingInstructions.map((val, ind) => {
        return (
          <tr>
            <td>{ind + 1}</td>
            <td>Smith</td>
            <td>50</td>
          </tr>
        )
      })}
    </div>
  }
    { console.log(props.rows.drivingInstructions) }
);
1
  • I don't see you actually returning anything from this component Commented Oct 3, 2020 at 17:38

3 Answers 3

3

if can't exist in an expression context. For similar reasons, you can't do:

const someVar = if (true) 10 else null;

The syntax is not permitted. To do something like this, you need the conditional operator:

export const Record = props => (
  !props.rows || !props.rows.drivingInstructions ? null : (
    <div className='card-container'>
      {props.rows.drivingInstructions.map((val, ind) => {
        ...

Or, if you're OK with the .card-container existing even if it doesn't have any rows, use optional chaining instead:

export const Record = props => (
  <div className='card-container'>
    {props.rows.drivingInstructions?.map((val, ind) => {
        ...
Sign up to request clarification or add additional context in comments.

5 Comments

Oh wow! thank you! I did not know about that rule and technique. The first solution still gave me the error "TypeError: Cannot read property 'map' of undefined" but your second solution worked 🙏🙏 that chaining technique is very cool!
My apologies, the first method also works, I just did not copy it properly 🤦‍♂️
could you please elaborate on the purpose or meaning of this code? !props.rows || !props.rows.drivingInstructions
If either of those are falsey, it evaluates to null. Otherwise, it evaluates the .map
Done! sorry about that, was unfamiliar with this platform too 😅
1

I would rewrite those code as below.

import React from 'react';

export const Record = (props) => {

  if (!!props?.rows?.drivingInstructions?.length) {

    return (<div className='card-container'>
      {props.rows.drivingInstructions.map((val, ind) =>(
          <tr>
            <td>{ind + 1}</td>
            <td>Smith</td>
            <td>50</td>
          </tr>
        )
      )}
    </div>);
   }

    return <div>No Driving Instructions</div>;
  }
   
};

This !!props?.rows?.drivingInstructions?.length is an optional chaining and it checks that those properties are not nullish. Lastly, it ensures the length is also more than 0 by converting to boolean using !!.

Comments

1
import React from 'react';

export const Record = props => (
   props?.rows?.drivingInstructions?.length ?
    (<div className='card-container'>
      {props.rows.drivingInstructions.map((val, ind) =>
          <tr>
            <td>{ind + 1}</td>
            <td>Smith</td>
            <td>50</td>
          </tr>
        )}
    </div>) : null;
);

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.