我在我的项目中使用了 usated 库。
在渲染方法中,我是这样使用的set
:
render() {
return (
<ApiSubscribe>
{api => (
<button content='CLICK ME' onClick={() => api.setMessage('RENDER CLICK')} />
)}
</ApiSubscribe>
)
}
如何调用api.setMessage
渲染的外部?例如在componentDidMount
?
Api订阅是:
export const ApiSubscribe = props => {
// We also leave the subscribe "to" flexible, so you can have full
// control over your subscripton from outside of the module
return <Subscribe to={props.to || [Api]}>{props.children}</Subscribe>;
};