简体   繁体   English

在反应绑定中渲染包含 JSX 的字符串

[英]Render string containing JSX in react binding

I've found a lot of questions that are similar to this but none that actually answer my question.我发现了很多与此类似的问题,但没有一个能真正回答我的问题。

I have a string that contains a JSX template to create a link and need to render this in the binding within a Text component.我有一个字符串,其中包含一个 JSX 模板来创建一个链接,并且需要在Text组件内的绑定中呈现它。 Below I have some same text so you can see approximately what I have.下面我有一些相同的文字,因此您可以大致了解我所拥有的内容。

Currently {content} will render as This string has JSX: [Object, object] .目前{content}将呈现为This string has JSX: [Object, object] How do I make it actually render FAQLink .我如何让它实际呈现FAQLink

What I've tried我试过的

I've tried using <React.Fragment>{content}<React.Fragment> with no luck.我试过使用<React.Fragment>{content}<React.Fragment>没有运气。

function TooltipContent() {
 const FAQLink = () => (
  <Link
    color="teal40"
    hoverColor="teal30"
    underlined
    to={paths.resourcePage({ slug: 'frequently-asked-questions' })}
  >
    FAQ
  </Link>
 );

 const contentArray = [
  `This string has JSX: ${<FAQLink />}`,
  `Another possible string`
 ];

 let content;
 if (variant === 'risk') {
  content = contentArray[0];
 } else {
  content = contentArray[1];
 }

 return (
  <Text textAlign="left" p={2}>
    {content}
  </Text>
 );
}

This code does work but isn't possible for my case此代码确实有效,但不适用于我的情况

function TooltipContent() {
 const FAQLink = () => (
  <Link
    color="teal40"
    hoverColor="teal30"
    underlined
    to={paths.resourcePage({ slug: 'frequently-asked-questions' })}
  >
    FAQ
  </Link>
 );

 return (
  <Text textAlign="left" p={2}>
    This string has JSX: <FAQLink />
  </Text>
 );
}

Unfortunately, template literals won't work with React components.不幸的是, template literals不适用于React组件。

However, you could utilize React.Fragment to accomplish what you need, like in the following example:但是,您可以使用React.Fragment来完成您需要的操作,如下例所示:

function TooltipContent(props) {
  const { variant = "default" } = props;
  const FAQLink = () => <a href="https://google.com">I am a link!</a>;

  let content;

  switch (variant) {
    case "risk":
      content = (
        <React.Fragment>
          RISK! <br />
          <FAQLink />
        </React.Fragment>
      );
      break;
    case "no-risk":
      content = "NO RISK";
      break;
    default:
      content = variant;
  }
  return <p>{content}</p>;
}

Here's a working example:这是一个工作示例:

Remember that JSX is just sugar syntax for React functions.请记住, JSX只是 React 函数的语法糖。 Your code transpiles like this:您的代码transpiles如下:

"use strict";

function TooltipContent() {
  var FAQLink = function FAQLink() {
    return React.createElement(Link, {
      color: "teal40",
      hoverColor: "teal30",
      underlined: true,
      to: paths.resourcePage({
        slug: 'frequently-asked-questions'
      })
    }, "FAQ");
  };

  var content = "This string has JSX: ".concat(React.createElement(FAQLink, null));
  return React.createElement(Text, {
    textAlign: "left",
    p: 2
  }, content);
}

So that concat function, or the ${} syntax, just converts your React elements (that is an object ) into a string , but this results in [Object object] .因此concat函数或${}语法只是将您的 React 元素(即object )转换为string ,但这会导致[Object object]

What you are trying to do, in this way, will always give you this result.以这种方式,您正在尝试做的事情总会给您这个结果。 Can you explain why you have to do this and you can not do in the other way?你能解释为什么你必须这样做而你不能以其他方式做吗?

如果我理解正确,您想将 FAQLink 呈现为代码示例,如果是,这里是代码示例: https ://codesandbox.io/s/render-jsx-text-u480p?fontsize=14&hidenavigation=1&theme=dark

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

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