简体   繁体   English

如何在React组件中将字符串呈现为子项?

[英]How do I render a string as children in a React component?

Take a simple component: 拿一个简单的组件:

function MyComponent({ children }) {
  return children;
}

This works: 这有效:

ReactDOM.render(<MyComponent><span>Hello</span></MyComponent>, document.getElementById('stage'));

but this doesn't (I removed the <span/> ): 但这没有(我删除了<span/> ):

ReactDOM.render(<MyComponent>Hello</MyComponent>, document.getElementById('stage'));

because React tries to call render on the string: 因为React尝试在字符串上调用render

Uncaught TypeError: inst.render is not a function

On the other hand, this works fine: 另一方面,这很好用:

ReactDOM.render(<p>Hello</p>, document.getElementById('stage'));

How do I make <MyComponent/> behave like <p/> ? 如何使<MyComponent/>表现得像<p/>

If you're using React 16.2 or higher, you can do this using React fragments: 如果您使用的是React 16.2或更高版本,则可以使用React片段执行此操作:

const MyComponent = ({children}) => <>{children}</>

If your editor doesn't support fragment syntax, this will also work: 如果您的编辑器不支持片段语法,这也将起作用:

const MyComponent = ({children}) =>
    <React.Fragment>{children}</React.Fragment>

Keep in mind that you're still creating & returning a component (of type MyComponent) as far as React is concerned - it just doesn't create an additional DOM tag. 请记住,就React而言,您仍在创建和返回组件(MyComponent类型) - 它只是不创建额外的DOM标记。 This is a mixed bag: 这是一个混合包:

  • Upside: you'll still see a <MyComponent> tag in the React Debug Tools. 好处:你仍会在React Debug Tools中看到<MyComponent>标签。

  • Downside: if you're using Typescript or Flow, your transpiler may complain, because MyComponent's return type is still a React element (React.ReactElement). 缺点:如果您正在使用Typescript或Flow,您的转换器可能会抱怨,因为MyComponent的返回类型仍然是React元素(React.ReactElement)。

Well the difference is <p> is an html element and MyComponent is a React Component. 差别是<p>是一个html元素而MyComponent是一个React Component。

React components need to render/return either a single component or a single html element. 反应的组分需要渲染/ 返回一个单个部件或一个单一的HTML元素。

'Hello' is neither. 'Hello'既不是。

You need at least one top-level HTML element. 您至少需要一个顶级HTML元素。 Your component can't really just output a string, that's not how React works. 您的组件实际上不能只输出字符串,这不是React的工作方式。

The simplest solution is to simply make your MyComponent wrap it's output in a span or div. 最简单的解决方案是简单地让MyComponent在span或div中包装它的输出。

function MyComponent({ children }) {
  return <span>{ children }</span>;
}

Currently, in a component's render, you can only return one node; 目前,在组件的渲染中,您只能返回一个节点; if you have, say, a list of divs to return, you must wrap your components within a div, span or any other component. 如果你有一个要返回的div列表,你必须将你的组件包装在div,span或任何其他组件中。

source 资源

And what you are returning is not a root node. 你要返回的不是根节点。 You are returning a react component that is returning a string where it should be returning an HTML element. 您正在返回一个react组件,它返回一个字符串,它应该返回一个HTML元素。

You can either pass your string already wrapped with an HTML element (like you already did in your example) or you can wrap your string in a HTML element inside your "MyComponent" like this 您可以传递已经用HTML元素包装的字符串(就像您在示例中所做的那样),也可以将字符串包装在“MyComponent”中的HTML元素中,就像这样

function MyComponent({ children }) {
  return <span>{ children }</span>;
}

React can render either React components (classes) or HTML Tags (strings). React可以呈现React组件(类)或HTML标签(字符串)。 Any HTML tag is by convention lowercase where a Component is Capitalized. 任何HTML标记按惯例都是小写的,其中Component是大写的。 Every React component has to render exactly one Tag (or null). 每个React组件都必须只呈现一个Tag(或null)。 To answer your question: you cannot. 回答你的问题:你做不到。

In the example above, you render what's given with the children attribute where this will render the tag inside or a string that is not valid. 在上面的示例中,您将呈现使用children属性给出的内容,其中这将在内部呈现标记或无效的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM