我有一个ScrollView内容不一定超过它的大小.让我们说这是一个表单,在出错时,它会扩展为添加错误消息然后超出ScrollView大小,这就是我使用该滚动的原因.
问题是我希望内容始终至少与ScrollView的大小相同.我知道该minHeight属性,但我发现使用它的唯一方法是重新渲染,我想避免.
我正在使用redux进行状态管理,这就是我所拥有的
<ScrollView
contentContainerStyle={[ownStyles.container, {minHeight: this.props.scrollHeight}]}
style={this.props.style}
showsVerticalScrollIndicator={false}
onLayout={(event) => {
scrollHeight = event.nativeEvent.layout.height;
this.props.setScrollContentHeight(scrollHeight);
}}
>Run Code Online (Sandbox Code Playgroud)
其中this.props.setScrollContentHeight触发入射的状态下的高度的动作,和this.props.scrollHeight被所述高度.因此,在触发onLayout函数的第一个渲染之后,使用ScrollView更新状态height,然后我将minHeight其分配给其内容.
如果我将内容的样式设置为,flex: 1则ScrollView将不会滚动.
你能想出一种避免第二次渲染以达到性能目的的方法吗?
谢谢!