我正在渲染很多Text组件并保存位置(以便滚动到它们),但是我想通过反跳来“捆绑”它来提高性能(并欢迎其他建议来改善性能) )。
...
import debounce from 'lodash/debounce'
...
class ContextProvider extends Component {
....
setContentPosition = (layout, key) => {
const contentPos = { ...this.state.contentPos }
contentPos[key] = layout.nativeEvent.layout.y
// TODO: make it debounce https://stackoverflow.com/questions/23123138/perform-debounce-in-react-js
this.setState({ contentPos }, () => console.log('contPos updated'))
}
...
render() {
return (
<Provider
value={{
setContentPosition: this.setContentPosition,
}}
>
{this.props.children}
</Provider>
)
}
}
我尝试了几种不同的组合,但是没有运气。希望它能起作用:
...
render() {
return (
<Provider
value={{
setContentPosition: debounce(this.setContentPosition, 200),
}}
>
{this.props.children}
</Provider>
)
}
}
它引发以下错误:
以下更改(针对contentPos[key]
)
setContentPosition = (layout, key) => {
const contentPos = { ...this.state.contentPos }
//console.log(layout.nativeEvent)
contentPos[key] = layout.nativeEvent
// TODO: make it debounce https://stackoverflow.com/questions/23123138/perform-debounce-in-react-js
this.setState({ contentPos }, () => {
console.log('contPos updated')
})
}
改为显示此警告:
该位置将用于滚动到文本组件(搜索时),并且我有一些测试代码可以滚动到某些Text
组件-在iOS上有效,但在Android上无效?