Skip to content

Better fix for base state bug#11273

Merged
acdlite merged 1 commit intofacebook:masterfrom
acdlite:fixbasestateagain
Oct 18, 2017
Merged

Better fix for base state bug#11273
acdlite merged 1 commit intofacebook:masterfrom
acdlite:fixbasestateagain

Conversation

@acdlite
Copy link
Collaborator

@acdlite acdlite commented Oct 18, 2017

This time with a test. Not super happy with the solution, but I think it's good enough for now.

},
);

it('uses correct base state for setState inside render phase', () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Before the fix, this would infinite loop. That's what was failing in www.

);

it('uses correct base state for setState inside render phase', () => {
spyOn(console, 'error');
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would be nice to check for specific message/count. Just in case.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated

queue1 = fiber.updateQueue = createUpdateQueue(fiber.memoizedState);
// TODO: We don't know what the base state will be until we begin work.
// It depends on which fiber is the next current. Initialize with an empty
// base state, then set to the memoizedState when rendering. Not super
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we just remove the argument to createUpdateQueue would it be that bad?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@gaearon I'd prefer to keep it for type safety for queues that don't belong to Fiber. Don't have any of those yet but could in the future (I do in one of my PRs).

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK

queue.expirationTime = NoWork;

let state = queue.baseState;
let state = queue.baseState === null
Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps comment should be moved here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll put it in both places :D

Copy link
Collaborator

@gaearon gaearon left a comment

Choose a reason for hiding this comment

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

I don't see anything obviously wrong. Which could mean there's still issues but at least we have one more regression test now :-)

@acdlite
Copy link
Collaborator Author

acdlite commented Oct 18, 2017

Ha indeed

@acdlite acdlite merged commit d964728 into facebook:master Oct 18, 2017
@gaearon
Copy link
Collaborator

gaearon commented Oct 18, 2017

Does this affect corner cases like this.state = null or this.replaceState(null)? Could it mislead them to follow the wrong path?

@acdlite
Copy link
Collaborator Author

acdlite commented Oct 18, 2017

@gaearon Don't think so, we have tests for those so I would hope not.

@acdlite
Copy link
Collaborator Author

acdlite commented Oct 18, 2017

Maybe replaceState(null)... that's an interesting one.

@gaearon
Copy link
Collaborator

gaearon commented Oct 18, 2017

Could use a sentinel instead of null?

@acdlite
Copy link
Collaborator Author

acdlite commented Oct 18, 2017

Yeah, didn't want to add an extra field. I'll try to think of something better and if I can't I'll do that.

@gaearon
Copy link
Collaborator

gaearon commented Oct 18, 2017

I mean something like

var NO_STATE_SENTINEL = {};
if (__DEV__) {
  if (Object.freeze) {
    Object.freeze(NO_STATE_SENTINEL);
  }
}

  let state = queue.baseState === NO_STATE_SENTINEL
    ? workInProgress.memoizedState
    : queue.baseState;

not sure if it makes sense

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants