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

implement a generic solution for setting a react key on a component from outside the component. #59

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

shyblower
Copy link

This has the advantage that neither every component that may be part of a sequence has to
implement a :keyfn function nor does the component have to know that fact and how to generate
a key which will not be needed by the component for any other purpose.

To keep the current API and since the key actually IS metadata this is accomplished
by setting metadata on the component's value at call time using the with-key
function, e.g. (MyComponent (with-key value key) ...), which will override :keyfn.

The only restriction for this to work is that the component's value has to be a datatype
which may carry metadata (implements the IWithMeta interface) like vectors, hashmaps, etc...

…rom outside the component.

This has the advantage that neither every component that may be part of a sequence has to
implement a :keyfn function nor does the component have to know that fact and how to generate
a key which will not be needed by the component for any other purpose.

To keep the current API and since the key actually IS metadata this is accomplished
by setting metadata on the components value at call time using the with-key
function, e.g. (MyComponent (with-key value key) ...), which will override :keyfn.

The only restriction for this to work is that the component's value has to be a datatype
which may carry metadata (implements the IWithMeta interface) like vectors, hashmaps, etc...
@shyblower
Copy link
Author

shyblower commented May 27, 2016

Another solution for setting the key from outside while keeping the API intact would be binding a dynamic var around the component call. While this would allow for scalar component values not implementing the IWithMeta interface on keyed components, it would have the disadvantage of every child of the component inheriting the same key unless rebound. While this should not affect the functionality of React's key mechanism since only siblings of an array or iterator (or a sequence in Clojurescript) must use distinct keys, it feels a bit awkward to me ... although I'm not entirely convinced that it actually is ...
... another problem may be that it will override the :keyfn function on its children though in my opinion the :keyfn mechanism shouldn't be used anyway as briefly laid out in my commit message.

@shyblower
Copy link
Author

I have improved the 'with-key' technique by using a deftype instead of metadata thus allowing to use also scalar values as component state.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant