小编典典

将道具从模板传递到react.js根节点

reactjs

我可能错过了一些东西,但是去了。如果我有:

var Swoosh = React.createClass({
  render: function() {
    return (
      <div className="swoosh">
        Boom.
      </div>
    );
  }
});

React.renderComponent(
  <Swoosh />,
  document.getElementById('content')
);

我可以props在挂载点(在id='content')上设置属性吗?

<div id='content' foo='alice' bar='has' bav='a cat' />
<!-- have foo, bar & bav available as props in <Swoosh />? -->

阅读 274

收藏
2020-07-22

共1个答案

小编典典

不,尽管您当然可以:

var container = document.getElementById('content');
React.renderComponent(
  <Swoosh
    foo={container.getAttribute('foo')}
    bar={container.getAttribute('bar')}
    bav={container.getAttribute('bav')} />,
  container
);

(或者,如果您想使用类的方法来创建属性字典,则可以执行Swoosh(attributes))。

2020-07-22