- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 React 中有一个非常简单的 If
组件:
'use strict';
var React = require('react');
module.exports = React.createClass({
render: function () {
if (this.props.condition) {
return this.props.children;
}
return false;
}
});
我可以这样调用它:
<If condition={someLogic}>
<div>Hi there</div>
</If>
问题是如果我在 If
组件中有多个标签:
<If condition={someLogic}>
<div>Container 1</div>
<div>Container 2</div>
</If>
这给了我一个错误:
Uncaught Error: Invariant Violation: exports.render(): A valid ReactComponent must be returned. You may have returned undefined, an array or some other invalid object.
这里 this.props.condition
是一个 ReactElement
数组。
问题:如何连接一组 ReactElement
并仅返回一个?
注意:我意识到我可以将这两个 div
放入一个包装器中,但为了这个示例(以及我的实际问题),我们可以这样说你不能这样做,并且你必须返回多个标签
最佳答案
React 不支持从渲染返回多个组件。渲染方法必须返回一个元素 - 您可以看到问题 https://github.com/facebook/react/issues/2127和 https://github.com/facebook/react/issues/2191
解决方案是通过某个元素包装 props.children,例如
var If = React.createClass({
render: function () {
if (this.props.condition) {
return <div>{this.props.children}</div>;
}
return false;
}
});
关于javascript - 连接 ReactElements 进行渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31388004/
将 ReactElement 作为属性传递有什么区别: First case } /> function RenderParam({ ReactElement }) { return {React
这更像是一个 Typescript和 React问题,但为了全面披露,我会提到我在尝试使用 react-navigation 创建自定义 header 时结束了这里。 . 我有一个简单的 ImageB
PanResponder 方法被传递给一个 ResponderSyntheticEvent,它的 nativeEvent.target 是一个 React 标签(它只是一个 Number)。这与 fi
我目前正在寻找通过循环通过该组件所有者通过 props 传递的数组来渲染类中的多个元素。这是一个例子: render() { return ( {th
我在 React 中有一个非常简单的 If 组件: 'use strict'; var React = require('react'); module.exports = React.createC
我正在读一篇section of the React docs它使用以下词汇,但我无法弄清楚它们之间的关系: 一个组件 组件的实例 组件的支持实例 虚拟 DOM 元素 一个ReactElement 它
我正在尝试使用 sweetalert-react 包 ( https://github.com/chentsulin/sweetalert-react ) 作为我的应用程序的模态。现在我让它工作了,但
在我的 JSX 中,我有一个条件渲染逻辑的情况 - 当元素 A 渲染某些东西时(它的 render() 函数返回 null 以外的东西),然后还渲染元素 B,就在元素 A 的上方。 代码示例(简化)如
我正在尝试将 Flow 与 ReactJS 结合使用。我需要为 prop children 添加类型。文章 ( Flow + React) 对此没有任何说明。 我发现了很多使用 ReactElemen
这段代码: import * as React from 'react';
我正在尝试添加这个简单的 div到我的 .tsx 文件的返回块: 我这样做: import React from 'react'; import Radium from 'radium'; impo
基本问题: 类型 React.ReactElement 与 JSX.Element 之间有什么区别?它们相同吗?我什么时候应该使用它们? 最佳答案 尽管 JSX 可以在 React 中使用来创建 Re
我有一个看起来像这样的组件。此版本完美运行: export default function StatusMessage(isAdded: boolean, errorMessage: string)
我有简单的Paragraph组件设置如下 import * as React from 'react'; import {Text} from 'react-native'; const { memo
我目前正在将 React 应用程序迁移到 TypeScript。到目前为止,这工作得很好,但我的 render 函数的返回类型有问题,特别是在我的功能组件中。 我一直使用 JSX.Element 作为
我有一个非常简单的测试 /** @jsx React.DOM */ var store = require('../simpleStore'); var TestUtils = require('re
当使用 React.SFC 时,typescript 出现以下错误: Type '({ children }: { children?: ReactNode; }) => Element' is no
我在 node_module 中收到多个 React.js 类型错误在构建步骤中: TS2320: Interface 'Element' cannot simultaneously extend t
我正在尝试将我的 App 组件传递到我的 Body 组件。它可以正常工作,而且似乎渲染得很好,但我在控制台中收到上述警告。这是我的代码: Document.js import React from '
我正在尝试使用 react v4 和 axios 发布帖子,我关注的是 this tutorial我完全按照他说的做了,但我收到了一个错误:可能是因为我使用的是其他版本的 react,请问如何解决?
我是一名优秀的程序员,十分优秀!