edu*_*edu 7 javascript reactjs
我想知道是否有办法通过使用某种类型的id或类型来获取组件,就像在DOM操作中一样.就像是:
var Avatar = React.createClass({
render: function () {
...
}
});
React.renderComponent(Avatar({id:'avatar'}), ...);
...
...
var avatar = React.getComponentById('avatar');
avatar.setProps({url = 'http://...'});
// or
var avatars = React.getComponentByType('Avatar');
if (avatars.length) {
avatars[0].setProps({url = 'http://...'});
}
Run Code Online (Sandbox Code Playgroud)
我不想保留组件实例的引用...
setProps是你应该谨慎使用的东西.事实上,存储对"渲染"组件的引用通常可能表明您可以以不同方式构造代码.我们还限制您使用setProps顶级组件.
var avatar = React.renderComponent(<Avatar .../>, node);
avatar.setProps({ foo: '1' });
Run Code Online (Sandbox Code Playgroud)
相当于这个,它与声明性模型更合适:
React.renderComponent(<Avatar .../>, node);
React.renderComponent(<Avatar ... foo="1" />, node);
Run Code Online (Sandbox Code Playgroud)
您可以在函数调用中包装该渲染,以便您可以随意调用它.
| 归档时间: |
|
| 查看次数: |
5475 次 |
| 最近记录: |