【发布时间】:2021-03-26 16:51:56
【问题描述】:
我有以下包装类型的条件包装,我正在尝试用条件包装替换以下条件
render: (text) => {
return (
<ConditionalWrapper
condition={isCheckedFn?.(text)}
wrapper={(ch) => (
<Badge count={<CheckCircleFilled style={{ color: 'green' }} />}>{ch}</Badge>
)}
>
<ConditionalWrapper
condition={isSelectable}
wrapper={(ch) => (
<Button type="link" onClick={() => action(ch)}>
{ch}
</Button>
)}
>
{isEqual(searchedColumn, fieldName) && !isEmpty(searchText) ? (
<Highlighter
highlightStyle={{ backgroundColor: '#ffc069', padding: 0 }}
searchWords={[searchText]}
autoEscape
textToHighlight={text.toString()}
/>
) : (
text
)}
</ConditionalWrapper>
</ConditionalWrapper>
);
}
下面是条件包装组件
const ConditionalWrapper = ({ condition, wrapper, children }) =>
condition ? wrapper(children) : children;
export default ConditionalWrapper;
然后我尝试用条件包装器替换下面的渲染荧光笔
{isEqual(searchedColumn, fieldName) && !isEmpty(searchText) ? (
<Highlighter
highlightStyle={{ backgroundColor: '#ffc069', padding: 0 }}
searchWords={[searchText]}
autoEscape
textToHighlight={text.toString()}
/>
) : (
text
)}
然后它看起来像这样
render: (text) => {
return (
<ConditionalWrapper
condition={isCheckedFn?.(text)}
wrapper={(ch) => (
<Badge count={<CheckCircleFilled style={{ color: 'green' }} />}>{ch}</Badge>
)}
>
<ConditionalWrapper
condition={isSelectable}
wrapper={(ch) => (
<Button type="link" onClick={() => action(ch)}>
{ch}
</Button>
)}
>
<ConditionalWrapper // getting error in this conditional wrapper
condition={isEqual(searchedColumn, fieldName) && !isEmpty(searchText)}
wrapper={(text) =>
(<Highlighter
highlightStyle={{ backgroundColor: '#ffc069', padding: 0 }}
searchWords={[searchText]}
autoEscape
textToHighlight={text.toString()}
/>)
}
text
/>
</ConditionalWrapper>
</ConditionalWrapper>
);
},
这段代码出现如下错误
错误:ConditionalWrapper(...):渲染没有返回任何内容。这 通常意味着缺少返回语句。或者,什么也不渲染, 返回空值。
我不知道我在上面的代码中做错了什么,谁能告诉我我做错了什么,这是使用条件包装器的正确方法。
在此先感谢
【问题讨论】:
-
还有什么不清楚的
标签: javascript reactjs conditional-statements