简体   繁体   English

如何使用渲染方法之外未声明的反应?

[英]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.

相关问题 如何在React中的render方法之外的函数中渲染组件? - How to render component from a function outside the render method in React? 未声明(反应)中的条件 - Conditionals in Unstated (React) 在reactjs中使用render方法之外的函数 - use a function outside render method in reactjs 如何在react类的render方法中使用回调函数中的数据 - How to use data from a callback function in the render method in a react class 如何在React JS渲染方法中使用变量? - How can I use a variable in React JS render method? 如何使用 React Router 替换 render 方法中的组件? - How to use React Router to replace a Component in render method? 如何在反应中使用render()? - How to use render () in react? 在 React 的 render 方法之外进行计算的最佳位置在哪里? - Where is the best place to make calculations outside the render method in React? React - “不要在组件的渲染方法中使用 HOC”是什么意思。 访问组件定义之外的 HOC。'? - React - What is meant by 'Do not use HOC’s in the render method of a component. Access the HOC outside the component definition.'? 结合使用Unstated和TypeScript和React,如何在内部获取类型化的容器实例 <Subscribe> 孩子? - Using Unstated with TypeScript and React, how to get a typed container instance inside <Subscribe> children?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM