在 RN ScrollView 中动态设置 scrollEnabled 为 False

nab*_*eel 2 react-native react-native-listview react-native-scrollview react-native-android

scrollEnabled当滚动到达某个位置时,如何更改React Native ScrollView param的 bool 值?

我确实在 ScrollView 中有一个 ScrollView,由于外部 ScrollView,内部 ScrollView 没有响应。当scrollEnabled参数设置为False外部 ScrollView时,内部 ScrollView 起作用。我的想法是将外部 ScrollView 动态设置为scrollEnabled={false}到达底部时。我该怎么做呢。

谢谢,

dol*_*s3m 7

有两种方法可以实现这一点。第一个,如@binchik 所述,是将 scrollEnabled 设置为状态变量并在必要时更新状态。当然,这会触发重新渲染,这可能是有问题的。第二种方式是直接更新ScrollView组件上的prop,无需重新渲染。你可以这样做:

class DisableScrollWithoutRerender extends React.Component {
  listRef = React.createRef();
  ...
  render() {
    return <ScrollView ref={this.listRef} />;
  }

  disableScroll() {
    const { current: list } = this.listRef;
    list.setNativeProps({ scrollEnabled: false });
  }
}
Run Code Online (Sandbox Code Playgroud)

就个人而言,我建议在重新渲染负担得起的情况下坚持使用第一个选项,并避免使用第二个选项,除非它正是您所需要的。

  • 我不得不使用 `this.listRef.setNativeProps({ scrollEnabled: false });` 而不是 `disableScroll()` (2认同)