在Context中反跳setState时遇到问题(本机)

时间:2018-10-12 20:33:36

标签: reactjs react-native ecmascript-6 lodash debounce

我正在渲染很多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>
    )
  }
}

它引发以下错误:

error message

更新01

以下更改(针对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')
    })
  }

改为显示此警告:

enter image description here

该位置将用于滚动到文本组件(搜索时),并且我有一些测试代码可以滚动到某些Text组件-在iOS上有效,但在Android上无效?

0 个答案:

没有答案