简体   繁体   English

如何将jsx元素存储到javascript变量

[英]how to store jsx element to javascript variable

I want to reuse my jsx element in multiple places.我想在多个地方重用我的 jsx 元素。

const value= <div>some text<Link href="abc.com">text</Link></div>

and I want to call value in multiple places.我想在多个地方调用价值。

I tried adding ' ' for the div but not getting element properly.我尝试为 div 添加“”,但没有正确获取元素。 It is returing as string.它作为字符串返回。 Can someone explain me the correct syntax to render jsx element to JavaScript variable?有人可以向我解释将 jsx 元素呈现为 JavaScript 变量的正确语法吗?

I want to reuse my jsx element in multiple places.我想在多个地方重用我的 jsx 元素。

You can't.你不能。 It can only be used in one place.它只能在一个地方使用。 It would appear that you can, I stand corrected by Mosè Raguzzini , this works just fine::看来你可以,我接受Mosè Raguzzini的纠正,这工作得很好::

const value= <div>some text<Link href="http://example.com">text</Link></div>;

ReactDOM.render(
  <div>
    {value}
    {value}
    {value}
  </div>,
  document.getElementById("root")
);

Live Example:现场示例:

 const value= <div>some text<a href="http://example.com">text</a></div>; ReactDOM.render( <div> {value} {value} {value} </div>, document.getElementById("root") );
 <div id="root"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.9.0/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.9.0/umd/react-dom.production.min.js"></script>

Another approach is to have a function that creates identical copies of it:另一种方法是使用 function 创建相同的副本:

const getValue = () => <div>some text<Link href="abc.com">text</Link></div>;

Then when you want one, use getValue() to get it.然后当你想要一个时,使用getValue()来获取它。

Or even give that a capital letter so it's a functional component:或者甚至给它一个大写字母,所以它是一个功能组件:

const Value = () => <div>some text<Link href="abc.com">text</Link></div>;

Live Example (of both):现场示例(两者):

 const getValue = () => <div>some text<a href="http://example.com">text</a></div>; const Value = () => <div>some text<a href="http://example.com">text</a></div>; ReactDOM.render( <div> {getValue()} {getValue()} <Value /> <Value /> </div>, document.getElementById("root") );
 <div id="root"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.9.0/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.9.0/umd/react-dom.production.min.js"></script>


how to store jsx element to javascript variable如何将jsx元素存储到javascript变量

You can't reuse a React element the way you've shown, but there's nothing wrong with:您不能按照您显示的方式重用React 元素,但没有任何问题:

const value= <div>some text<Link href="abc.com">text</Link></div>

(other than relying on ASI at the end, because there's no ; ). (除了最后依赖 ASI,因为没有; )。 That works just fine.这工作得很好。 But it doesn't make it reusable.但这并不能使其可重复使用。

Here's an example doing that:这是一个这样做的例子:

 const value= <div>some text<a href="http://example.com">text</a></div>; ReactDOM.render( <div> {value} </div>, document.getElementById("root") );
 <div id="root"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.9.0/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.9.0/umd/react-dom.production.min.js"></script>

You could create a stateless component to reuse said component:您可以创建一个无状态组件来重用所述组件:

  import React from "react"

  export const MyComponent = () => (
    <div>
      some text <Link href="abc.com">text</Link>
    </div>
  );

I assume that you want to send dynamic content to the Links, so made href as ref:)我假设您想将动态内容发送到链接,因此将 href 设为 ref:)


function MyFunction(){




const Value = ({ href }) => <div>some text<Link href={ href }>text</Link></div>;


 return (
 <div>
    <p> div with value and more </p>
    <Value href={"a.com"} />
    <Value href={"b.com"} />
    <Value href={"c.com"} />
  </div>
    )
}

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

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