Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TimerMixin] Remove TimerMixin from ListView #21488

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions Libraries/Lists/ListView/ListView.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const RCTScrollViewManager = require('NativeModules').ScrollViewManager;
const ScrollView = require('ScrollView');
const ScrollResponder = require('ScrollResponder');
const StaticRenderer = require('StaticRenderer');
const TimerMixin = require('react-timer-mixin');
const View = require('View');
const cloneReferencedElement = require('react-clone-referenced-element');
const createReactClass = require('create-react-class');
Expand Down Expand Up @@ -216,14 +215,15 @@ type Props = $ReadOnly<{|

const ListView = createReactClass({
displayName: 'ListView',
_rafId: (null: ?AnimationFrameID),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_rafIds: Array<AnimationFrameID>

_childFrames: ([]: Array<Object>),
_sentEndForContentLength: (null: ?number),
_scrollComponent: (null: ?React.ElementRef<typeof ScrollView>),
_prevRenderedRowsCount: 0,
_visibleRows: ({}: Object),
scrollProperties: ({}: Object),

mixins: [ScrollResponder.Mixin, TimerMixin],
mixins: [ScrollResponder.Mixin],

statics: {
DataSource: ListViewDataSource,
Expand Down Expand Up @@ -347,16 +347,23 @@ const ListView = createReactClass({
contentLength: null,
offset: 0,
};

exced marked this conversation as resolved.
Show resolved Hide resolved
this._childFrames = [];
this._visibleRows = {};
this._prevRenderedRowsCount = 0;
this._sentEndForContentLength = null;
},

componentWillUnmount: function() {
if (this._rafId != null) {
cancelAnimationFrame(this._rafId);
}
},
exced marked this conversation as resolved.
Show resolved Hide resolved

componentDidMount: function() {
// do this in animation frame until componentDidMount actually runs after
// the component is laid out
this.requestAnimationFrame(() => {
this._rafId = requestAnimationFrame(() => {
Copy link
Contributor

@RSNara RSNara Oct 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both componentDidMount and componentDidUpdate will write to this._rafId, which means that the rafId from componentDidUpdate will overwrite the rafId from componentDidMount. Suppose that both hooks are executed, and then the component is unmounted, all before the first requestAnimationFrame hook is executed. Then, with the current setup, we'll end up executing this._measureAndUpdateScrollProps(); once.

To get around this, we could add the following function to ListView:

requestAnimationFrame(fn: () => void): void {
  const rafId = requestAnimationFrame(() => {
    this._rafIds.splice(this._rafIds.indexOf(rafId));
    fn();
  })
  this._rafIds.push(rafId);
},

this._measureAndUpdateScrollProps();
});
},
Expand Down Expand Up @@ -384,7 +391,7 @@ const ListView = createReactClass({
},

componentDidUpdate: function() {
this.requestAnimationFrame(() => {
this._rafId = requestAnimationFrame(() => {
this._measureAndUpdateScrollProps();
});
},
Expand Down