Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Add key to rendered components, so React doesn't optimize #101

Merged
merged 5 commits into from
Dec 14, 2018

Conversation

valentijnnieman
Copy link
Contributor

@valentijnnieman valentijnnieman commented Nov 27, 2018

This is an interesting fix, where we're now passing a key to the rendered components, so that React doesn't try to optimize and recognizes that the components are different. This is an issue for having multiple Graph components for example, where if there's no key, React will try to optimize and will use the same state for both Graphs. This is what's happening in dcc #379, and this PR will fix that.

Link to the React docs: https://reactjs.org/docs/lists-and-keys.html#keys

@chriddyp
Copy link
Member

Can we add an integration test?

@valentijnnieman
Copy link
Contributor Author

@chriddyp I didn't add any integration tests here because these are React internals - what should it test? Should it test that the React component's constructor() is called for each new Graph component? Or test that React passes a key prop correctly to the components? I guess I can add a test that tests if click data is being passed through to a callback, when there's multiple Graph components, but that feels more like a DCC test to me.

@chriddyp
Copy link
Member

I guess I can add a test that tests if click data is being passed through to a callback, when there's multiple Graph components, but that feels more like a DCC test to me.

That's what I'd recommend testing. Basically, we have an example that is broken right now (tabs in a graph) because of dash-renderer. This PR proposes that it fixes this example, so we should add a test that mimics that example to "prove" that it indeed fixes the issue.

@chriddyp
Copy link
Member

Or test that React passes a key prop correctly to the components?

This in and of itself isn't important for me to test as we have to infer that adding a key will prevent something from using the same instance which will prevent odd event handling bug code like this. So, I'd rather test a step further back with the actual tabs and graphs.

@chriddyp
Copy link
Member

chriddyp commented Nov 29, 2018

Could we reorder the commits so that the test is first? That way we can make sure that the test is failing on CircleCI and then is subsequently fixed with the next commit. Once my comments are considered, 💃

@valentijnnieman
Copy link
Contributor Author

Thought I'd mention that I also considered passing in the key prop in the component that NotifyObservers spits out - via extraProps here that fixes the bug too, and puts the key right on the component instead of on it's parent.

@chriddyp
Copy link
Member

chriddyp commented Dec 4, 2018

Since this is taking a little longer to get through review, could we create a pre-release so that any community members or customers that are affected by this bug can try out the prerelease?

Refactored and cleaned-up test

Cast to string to remove 'u' for tests

Cast el.text to string in wait_for_text_to_equal method

Try expected clickData as Python dict and json.dump it
CHANGELOG.md Outdated
@@ -2,6 +2,10 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [0.16.1] - 2018-11-27
### Added
Copy link
Contributor

Choose a reason for hiding this comment

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

Internally this is added -- externally this is a bug that's been fixed. Change to ```### Fixed`

Copy link
Contributor

@Marc-Andre-Rivet Marc-Andre-Rivet left a comment

Choose a reason for hiding this comment

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

Could we reorder the commits so that the test is first? That way we can make sure that the test is failing on CircleCI and then is subsequently fixed with the next commit.

I agree we should see this test failing without the fix before concluding the fix is correct -- the test is very remote from the code fix and we have to infer a lot. Running local CircleCI CLI with and w/o fix shows that the fix allows the test to pass. Manually tested the test, this look good. 💃

In the future, we should consider creating test components that live inside of the dash repo to test for specific scenarios like this. These components will provide stable, no-surprise, no-dependency implementations for specific test cases (for example this could be a simple Dash Component that renders state.value which is copied from props.value in the ctor, guaranteeing the keys have the expected behavior)

Co-Authored-By: valentijnnieman <valentijnnieman@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants