简体   繁体   English

使用react.js呈现自定义html标记

[英]Rendering custom html tag with react.js

What I'm trying to do is quite easy at first however I get an (obviously completely useless) error from webpack and I'm wondering how it can be fixed, I want a simple "custom" tag to be rendered by React, the code is as follows: 我想要做的事情很简单,但是我从webpack得到一个(显然完全无用的)错误,我想知道它是如何修复的,我想要一个简单的“自定义”标签由React渲染,代码如下:

          let htmlTag = "h" + ele.title.importance;
          let htmlTagEnd = "/h" + ele.title.importance;
          return(
            <{htmlTag} key={elementNumber}>{ele.title.content}<{htmlTagEnd}>
          );

Basically instead of having a predefined tag I want to have my own {template} tag, I know in this situation there would be work arounds for this (eg defining a className with my "importance" value and adding some css for that), but for the sake of science I'd like to know how (and if) this can be done in react/jsx. 基本上我没有预定义的标签,而是希望拥有自己的{template}标签,我知道在这种情况下会有解决方法(例如用我的“重要性”值定义一个className并为此添加一些css),但是为了科学,我想知道如何(和如果)这可以在react / jsx中完成。

JSX doesn't allow you to use dynamic HTML tags (dynamic components would work). JSX不允许您使用动态 HTML标记(动态组件可以工作)。 That's because whenever you use something like <sometag ... /> , an HTML element with tag name sometag is created. 这是因为无论何时使用像<sometag ... />这样的东西,都会创建一个标签名为sometag的HTML元素。 sometag is not resolved as a variable. sometag未被解析为变量。

You also can't do what you have shown above. 你也做不到你上面所示的。 JSX expressions are not valid in place of a tag name . JSX表达式无效代替标记名称

Instead, you have to call React.createElement directly: 相反,您必须直接调用React.createElement

return React.createElement(
  "h" + ele.title.importance,
  {
    key: elementNumber,
  },
  ele.title.content
);

Edit 编辑

My initial answer was not correct, you cannot use a variable directly and would need to use the createElement method described in Felix's answer . 我的初步答案不正确,你不能直接使用变量,需要使用Felix的答案中描述的createElement方法。 As noted below, and utilised in the blog post I originally linked, you can use object properties, so I've made an example of this, which hopefully will be useful as an answer to the question. 如下所述,并在我最初链接的博客文章使用,您可以使用对象属性,所以我已经做了一个这样的例子,希望这将有用作为问题的答案。

class Hello extends React.Component {
    constructor() {
    super();
    this.state = {
        tagName: "h1"
    };
  }

  sizeChange(i) {
    this.setState({
        tagName: 'h' + i
    });
  }

  changeButtons() {
    var buttons = [];
    for (let i=1; i<=6; i++) {
        buttons.push(<button onClick={() => this.sizeChange(i)}>H{i}</button>);
    }
    return buttons;
  }

  render() {
    return (
    <div>
      {this.changeButtons()}
        <this.state.tagName>
        Change Me
      </this.state.tagName>
    </div>
    );
  }
}

JSFiddle here JSFiddle在这里

Original Answer 原始答案

It can be done, although I don't think it is officially supported so may break in the future without warning. 它可以做到,虽然我不认为它是官方支持的,所以未来可能会在没有警告的情况下破坏。 The caveat to this approach is that the variable name you choose for your tag cannot be the same as an HTML element. 这种方法的警告是,您为标记选择的变量名称不能与HTML元素相同。

var Demo = React.createClass({
    render: function() {
       const elementTag = 'h' + ele.title.importance;
       return(
           <elementTag>
              Header x contents
           </elementTag>
       );
    }
});

More explanation and a fuller example can be found here 这里可以找到更多解释和更全面的例子

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

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