我可能错过了一些东西,但这里有.如果我有:
var Swoosh = React.createClass({
render: function() {
return (
Boom.
);
}
});
React.renderComponent(
,
document.getElementById('content')
);
我可以props
在挂载点(where id='content'
)上设置属性吗?
Sophie Alper.. 19
不,当然你可以这样做:
var container = document.getElementById('content'); React.renderComponent(, container );
(或者如果你想使用像/sf/ask/17360801/这样的属性dict ,那么你可以这样做Swoosh(attributes)
).
API中没有任何内容可以将属性从普通DOM元素传输到React组件,但您可以创建一个Mixin来执行此操作.请注意,这只适用于传递给renderComponent
它的组件,因为它使用setProps
:
(工作JSFiddle)
var InheritsDomAttributes = { componentDidMount: function(rootNode) { var hasNextProps = false; var nextProps = {}; var parentNode = rootNode.parentNode; Object.keys(parentNode.attributes).forEach(function(key) { var namedNode; // NamedNodeMaps have an attribute named "length" that // should not be considered a set attribute. if (key !== "length") { hasNextProps = true; namedNode = parentNode.attributes[key]; nextProps[namedNode.name] = namedNode.value; } }); if (hasNextProps) this.setProps(nextProps); } }; var Swoosh = React.createClass({ mixins: [InheritsDomAttributes], render: function() { return ( <div className="swoosh"> Boom. </div> ); } }); React.renderComponent( <Swoosh />, document.getElementById('content') );
不,当然你可以这样做:
var container = document.getElementById('content'); React.renderComponent( <Swoosh foo={container.getAttribute('foo')} bar={container.getAttribute('bar')} bav={container.getAttribute('bav')} />, container );
(或者如果你想使用像/sf/ask/17360801/这样的属性dict ,那么你可以这样做Swoosh(attributes)
).