如何在render方法之外使用react unstated?

gai*_*bou 6 reactjs unstated

我在我的项目中使用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)

azi*_*ium 6

像这样?

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)