我在我的项目中使用ustated库。
在渲染方法中,我这样使用set:
render() {
return (
<ApiSubscribe>
{api => (
<button content='CLICK ME' onClick={() => api.setMessage('RENDER CLICK')} />
)}
</ApiSubscribe>
)
}
Run Code Online (Sandbox Code Playgroud)
如何api.setMessage在渲染外调用?例如在componentDidMount?
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>;
};
Run Code Online (Sandbox Code Playgroud)
像这样?
class Child extends Component {
componentDidMount() {
this.props.api.setMessage('hey')
}
render {...}
]
let Parent = () => (
<ApiSubscribe>
{api => <Child api={api} />}
</ApiSubscribe>
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1027 次 |
| 最近记录: |