[英]Returning boolean from arrow function in a react functional component
I'm trying to extract the business logic from my stateless functional components in React 16.2. 我正在尝试从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: 当我想提取
isInitJobExpanded
,我尝试了:
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. 提前致谢。
isInitJobExpandedFunction
receive an object, that must have two fields: entry
and props
. isInitJobExpandedFunction
接收一个对象,该对象必须具有两个字段: entry
和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} /> )));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.