javascript - 函数作为 React 子级无效。我在我的代码中找不到导致此错误的任何内容
问题描述
我有包含很多逻辑的父组件和许多其他组件,它们是仅返回 JSX 方法的功能组件。我收到了很多“函数作为 React 孩子无效”的错误,所以我进行了更改以修复此问题,我认为更改{FunctionalComponent}
为{() => FunctionalComponent()
应该解决此问题,是的,大多数错误都消失了,但必须还有一件事导致此错误。我试图调试我的代码,但调试器没有进入子(功能组件)返回方法。
我将在这里介绍主要组件的渲染方法,以及示例子组件。也许我做错了,因为我之前没有使用功能组件。
为我的过滤器面板生成快速过滤器的主要组件渲染方法:
generateQuickFilters = () => {
return this.filterTypes.map(filter => {
const options = {
filter: filter,
maxFilterOptions: this.state.maxFilterOptions,
toggle: this.toggle,
toggleCollapse: this.toggleCollapse,
onShowMoreButtonClick: this.onShowMoreButtonClick,
hoverIn: this.hoverIn,
hoverOut: this.hoverOut,
analytics: this.state.analytics
};
return (<QuickFilter options={options} key={UUID.UUID()}/>);
});
}
render(): React.ReactNode {
return (
<div>
{this.generateQuickFilters()}
<QuickFiltersModal visible={this.state.isModalVisible} onConfirmClick={this.hideModal}/>
</div>
);
}
这是一个快速过滤器的外观:
export interface QuickFilterPropsOptions {
toggleCollapse: (subtype: any) => void;
toggle: (subtype: any) => void;
hoverIn: (subtype: any) => void;
hoverOut: (subtype: any) => void;
filter: any;
analytics: Analytics;
maxFilterOptions: number;
onShowMoreButtonClick: (subtype: any) => void;
}
export interface QuickFilterProps {
options: QuickFilterPropsOptions;
}
export class QuickFilter extends React.PureComponent<QuickFilterProps, {}> {
constructor(props: QuickFilterProps, context: any) {
super(props, context);
}
render(): React.ReactNode {
return (
<>
<div key={UUID.UUID()}>
<div className='row'>
{() => QuickFilterHeader(this.props.options)}
{() => QuickFilterOption(this.props.options)}
{() => QuickFilterFooter(this.props.options)}
</div>
</div>
</>
);
}
例如,我将在此处粘贴使用其他功能组件的 QuickFilterOption 功能组件:
export function QuickFilterOption(props) {
return (
<>
<table>
{() => OptionLabel(props)}
{() => OptionSubtypeHeader(props)}
<tbody>
{() => OptionValues(props)}
</tbody>
</table>
</>
);
这是创建元素 DOM 树的正确方法吗?知道是什么导致了这个错误吗?也许我不应该使用功能组件?
编辑1:
export function OptionValues (props) {
const generateValue = () => {
// For each subtype
props.filter.subtypes.map((subtype) => {
// Checkbox is disabled when it relates to a property that has no connections or calls
const disabled = props.option.properties[subtype.property] === undefined; // || option.properties[subtype.property].calls === 0;
// Checkbox is selected when it is disabled or has a selected property set to true
const selected = disabled || props.option.properties[subtype.property].selected;
const classNames: string[] = [];
// Check if needed
if (selected) {
classNames.push('selected');
}
const optionValue = () => {
if (selected) {
props.option.properties[subtype.property].selected = true;
props.updateQuickFilters$.next();
} else {
props.option.properties[subtype.property].selected = false;
props.updateQuickFilters$.next();
}
};
// TODO TOOLTIP
return (
<td>
<div className={'ff-checkbox clickable'}><input type={'checkbox'} className={classNames.join(' ')}
disabled={disabled} onClick={optionValue}/></div>
</td>);
});
}
return (
<>
{() => generateValue()}
</>);
}
解决方案
您有几个地方提供子功能,例如<div className='row'>
in QuickFilter
:render
<div className='row'>
{() => QuickFilterHeader(this.props.options)}
{() => QuickFilterOption(this.props.options)}
{() => QuickFilterFooter(this.props.options)}
</div>
里面的孩子<div className='row'>
都是函数。也许你的意思是:
<div className='row'>
{QuickFilterHeader(this.props.options)}
{QuickFilterOption(this.props.options)}
{QuickFilterFooter(this.props.options)}
</div>
...虽然实际上,最好将它们用作组件(对处理它的函数进行任何必要的更改),例如:
<div className='row'>
<QuickFilterHeader options={this.props.options}/>
<QuickFilterOption options={this.props.options}/>
<QuickFilterFooter options={this.props.options}/>
</div>
你有同样的东西QuickFilterOption
。
推荐阅读
- linux - 为什么内存地址在 Ubuntu 而不是 Redhat 中发生变化
- mysql - 原始序列中需要的 MySQL FIND_IN_SET 搜索结果
- c# - .NET Core 中的 NLog 事件属性
- angular - 如何将 Angular Material Table 数据刷新回页面中的初始状态?
- django - 如何更改graphene-python(中继)中的连接参数(之后,之前)?
- r - 由第二个变量加权的一个变量的标准偏差
- python - 如何检查一个元素是否存在于python的多个列表中
- xamarin - 当应用程序在后台时保持任务工作
- google-chrome - 如何将文本发送到 Google Hangouts 窗口,然后 alt-tab 到另一个窗口并继续在 AutoHotKey 中将文本发送到最小化窗口?
- c++ - 在 C++ 中使用数字和条件填充向量