0

I'm trying to extract the business logic from my stateless functional components in React 16.2.

This works:

const Jobs = props => (
  props.entries
    .map(entry => (
      <Job
        entry={entry}
        isInitJobExpanded={
          entry.fields.project['en-US'] &&
          props.initJobExpanded &&
          (entry.fields.project['en-US'] === props.initJobExpanded)}
        key={entry.sys.id}
      />
    )));

And when I want to extract isInitJobExpanded, I've tried:

const isInitJobExpandedFunction = ({ entry, props }) => (
  entry.fields.project['en-US'] &&
  props.initJobExpanded &&
  entry.fields.project['en-US'] === props.initJobExpanded
);

const Jobs = props => (
  props
    .map(entry => (
      <Job
        entry={entry}
        isInitJobExpanded={isInitJobExpandedFunction}
        key={entry.sys.id}
      />
    )));

However, I get the error message:

Invalid prop `isInitJobExpanded` of type `function` supplied to `Job`, expected `boolean`.`

I've also tried:

isInitJobExpanded={this.isInitJobExpandedFunction}

isInitJobExpanded={this.isInitJobExpandedFunction()}

isInitJobExpanded={isInitJobExpandedFunction()}

isInitJobExpanded={() => isInitJobExpandedFunction}

etc

Thanks in advance.

1
  • 1
    isInitJobExpanded={isInitJobExpandedFunction({entry, props)} Commented Feb 18, 2018 at 13:38

1 Answer 1

1

isInitJobExpandedFunction receive an object, that must have two fields: entry and props. So your code is:

const isInitJobExpandedFunction = ({ entry, props }) => (
  entry.fields.project['en-US'] &&
  props.initJobExpanded &&
  entry.fields.project['en-US'] === props.initJobExpanded
);

const Jobs = props => (
  props
    .map(entry => (
      <Job
        entry={entry}
        isInitJobExpanded={isInitJobExpandedFunction({entry, props})}
        key={entry.sys.id}
      />
    )));

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I tried to pass it with {isInitJobExpandedFunction(entry, props)} not {isInitJobExpandedFunction({entry, props})}. Is there a rule of thumb on when to pass it in curly braces or not with arrow functions?
It depends on the function declaration. You also can declare you arrow function as: const isInitJobExpandedFunction = (entry, props) => {} and call it without curly bracers: isInitJobExpandedFunction(entry, props)

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.