【问题标题】:Curried function not getting called in componentDidMount没有在 componentDidMount 中调用 Curried 函数
【发布时间】:2018-08-16 18:44:27
【问题描述】:
componentDidMount() {
    this.onRefresh(this.props.subdomainConfig)
}

onRefresh = config => () => {
    console.log('onRefresh', config)
}

在这个反应组件中,当组件安装时,onRefresh 函数根本不会被调用。如果我将此函数用于 RefreshControl,那么它会被调用。

<ScrollView
    refreshControl={
        <RefreshControl
            refreshing={this.state.refreshing}
            onRefresh={this.onRefresh(this.props.subdomainConfig)}
        />
    }>
    <ZoneListComponent
        zones={zones}
        onPressEdit={this.onPressEdit}
    />
</ScrollView>

如果有人能告诉我为什么在 componentDidMount 中没有调用它,但对 RefreshControl 有效。

【问题讨论】:

  • this.onRefresh(this.props.subdomainConfig) 返回一个函数,要调用它,您需要在末尾添加一对括号。
  • 是的!现在明白了:)

标签: reactjs react-native currying react-lifecycle


【解决方案1】:

onRefresh 返回一个函数但不调用它。将其更改为

componentDidMount() {
    this.onRefresh(this.props.subdomainConfig)();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    • 2019-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多