0

I'm rendering a lot of Text components and saving positions (in order to scroll to them), but I would like to improve performance by "bundling" it by a debounce (and are open for other suggestions that would improve performance).

  ...
  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>
    )
  }
}

I have tried a couple of different combinations, without luck. Was expecting this to work:

  ...
  render() {
    return (
      <Provider
        value={{
          setContentPosition: debounce(this.setContentPosition, 200),
        }}
      >
        {this.props.children}
      </Provider>
    )
  }
}

It throws the following error:

error message

Update 01

The following change (for 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')
    })
  }

displays this warning instead:

enter image description here

The position is to be used for scrolling to a text component (when searching) and I have some testing code that scrolls to some Text component - works on iOS but not on Android?

Norfeldt
  • 8,272
  • 23
  • 96
  • 152

0 Answers0