简体   繁体   English

使用ReactJS createElement在不使用JSX的情况下输出HTML

[英]Using ReactJS createElement to output HTML without JSX

I am trying to create this code in ReactJS without JSX 我正在尝试在没有JSX的 ReactJS中创建此代码

<li>
  <a href="javascript:;" onClick={onClick ? this.handleClick : null}>
    <div className="home-gallery" style={{background: `url(${photo})`}}/>
  </a>
</li>

Reason is due to altering previous code in a component that doesn't quite fit my needs. 原因是由于更改了不完全符合我需求的组件中的先前代码。 Reading some other posts I came to this but its far from working. 在阅读其他文章时,我谈到了这一点,但远没有奏效。

_createClass(Photo, [{
 key: 'handleClick',
  value: function handleClick(event) {
   var _props = this.props,
      onClick = _props.onClick,
        index = _props.index,
        photo = _props.photo;

      onClick(event, { photo: photo, index: index });
  }
}, {
    key: 'render',
    value: function render() {
      var _props2 = this.props,
            photo = _props2.photo,
          onClick = _props2.onClick,
           margin = _props2.margin;

      var imgStyle = { background: `url(${photo})`, margin: margin };
      return 

      _react2.default.createElement('li', null,
        _react2.default.createElement('a', _extends({
          onClick: onClick ? this.handleClick : null
        },
          _react2.default.createElement('div', _extends({
            style: onClick ? _extends({}, imgStyle, imgWithClick) : imgStyle
          }))
        ))
      )
  }}]);

Could someone point me in the direction of fixing this or reference how to best learn what I am doing wrong. 有人可以指出我的解决方向还是参考如何最好地了解我在做什么错。

UPDATE 更新

I have no figured out the majority of my query with altering to but the background: 'url(${photo})' is still not producing, whilst the margin is. 我没有想出将查询更改为大多数查询,但是background: 'url(${photo})'仍然没有生成,而边距却是。

var imgStyle = { background: `url(${photo})`, margin: margin };

      return _react2.default.createElement(
        "li",
        null,
        _react2.default.createElement(
          "a",
          { href: "javascript:;", onClick: onClick ? this.handleClick : null },
          _react2.default.createElement("div", { className: "home-gallery", style: onClick ? _extends({}, imgStyle) : imgStyle })
        )
      );

Okay, I came to the solution by using the online Babel compiler . 好的,我通过使用在线Babel编译器来解决该问题 Putting in the JSX gave an output that lead me toward the solution. 放入JSX可以得到输出,引导我找到解决方案。

var imgStyle = { backgroundImage: 'url(' + photo.src + ')', margin: margin };
  console.log(photo)
  return _react2.default.createElement(
    "li",
    null,
    _react2.default.createElement(
      "a",
      { href: "javascript:;", onClick: onClick ? this.handleClick : null },
      _react2.default.createElement("div", { className: "home-gallery", style: onClick ? _extends({}, imgStyle) : imgStyle })
    )
  );

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

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