-
-
Notifications
You must be signed in to change notification settings - Fork 51
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
Effectively resetting state data #120
Comments
After I think about this more, I think the key thing I'm missing, is the fact that I cannot force a component that uses This is something I often do with routing. I.e., // for route /something/{id}
const { id } = useParams() as { id: string };
//...
<SomeComponent key={id} id={id} /> If the user navigates between these two routes:
The matching route of So what I'm saying, is it would be great if when Does that make sense? |
Hey @JasonStoltz , the right answer to cleanly separate the data of the two records is to use a key like you wrote in point 2 in your original post... and that means calling I just finished a draft PR that lets you use one specific keyed logic without passing the props down a component chain. This is done with React Context. I wrote a draft blog post about the feature as well. I'd be very curious to hear if that can solve your usecase. Apologies for the long delay in my reply though :) |
@mariusandra Oh wow, this is a great solution. Thanks for the follow up! |
So this is now live in kea 2.3 as a I'm also very curious to hear if this solved the original problem or not. |
I'm using a logic in a React component. This is a super simplified example:
Assume that
fetchData
is a listener that fetches a record and stores it in therecord
field.And let's say this component is rendered with an
id
of1
. Data is fetched and the id of record1
has been rendered.The
id
prop then changes to2
.fetchData
is called and then the id of record2
is rendered.However, on that second render, just after the
id
prop changes to2
, there is a brief period where theid
prop is2
, but the record is still the old record, which is record1
. And it stays that way untilfetchData
completes and the current record is updated.I'm trying to avoid that.
What I'd really like to happen, is when
id
changes, I'd like to the logic to be completely reset. How can I accomplish that?A couple of thoughts I've had:
key
to SomeComponent, so like<SomeComponent key={id} id={id} />
which should cause SomeComponent to remount, which I was hoping would also cause the logic to remount as well... but that doesn't seem work. SomeLogic still doesn't seem to be remounted.useValue(SomeLogic({ id })
. Which I think would work ... but the problem I find there which is not apparent in this example ... is in all the nested components where I want to access SomeLogic.record, I always need to pass that key, so I end up passing that key all around my code which is not ideal.Is there some way to force that logic to remount?
Any insight would be appreciated.
Thanks!
The text was updated successfully, but these errors were encountered: