[英]How to use react unstated outside of render method?
I am using ustated library in my project. 我在我的项目中使用了ustated库。
In render method, I am using set
like this: 在render方法中,我使用如下set
:
render() {
return (
<ApiSubscribe>
{api => (
<button content='CLICK ME' onClick={() => api.setMessage('RENDER CLICK')} />
)}
</ApiSubscribe>
)
}
How can I call api.setMessage
OUTSIDE of render? 如何在渲染中调用api.setMessage
OUTSIDE? For example in componentDidMount
? 例如在componentDidMount
?
ApiSubscribe is: ApiSubscribe是:
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>;
};
Like this? 像这样?
class Child extends Component {
componentDidMount() {
this.props.api.setMessage('hey')
}
render {...}
]
let Parent = () => (
<ApiSubscribe>
{api => <Child api={api} />}
</ApiSubscribe>
)
您可以创建一个HOC来包装您的组件,然后将容器从HOC组件以props的形式传递给子组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.