For AI agents: the complete documentation index is available at /next/zh/llms.txt, the full documentation bundle is available at /next/zh/llms-full.txt, and this page is available as Markdown at /next/zh/api/react/Functions/withInitDataInState.md.
  • 简体中文
  • 函数: withInitDataInState()

    withInitDataInState<P, S>(App: ComponentClass<P, S>): ComponentClass<P, S>

    高阶组件(HOC),将 initData 注入到给定类组件的 state 中。

    该 HOC 会检查传入的组件是否为类组件。如果是,则包装该组件,并将 initData 注入其 state。同时它会添加一个监听器,在数据变化时更新 state,并在组件卸载时移除该监听器。

    类型参数

    类型参数描述
    P被包裹组件的 props 类型。
    S被包裹组件的 state 类型。

    参数

    参数类型描述
    AppComponentClass<P, S>被该高阶组件包裹的类组件。

    返回

    ComponentClass<P, S>

    若传入的不是类组件,则返回原组件;否则返回一个新的类组件, 该组件具备 initData 注入和 state 更新功能。

    示例

    class App extends React.Component<MyProps, MyState> {
      // component implementation
    }
    
    export default withInitDataInState(App);
    除非另有说明,本项目采用知识共享署名 4.0 国际许可协议进行许可,代码示例采用 Apache License 2.0 许可协议进行许可。