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

Navigation refresh the store, but the web-components using derived does not receive the new value. #458

Closed
AlbertSabate opened this issue Sep 7, 2024 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@AlbertSabate
Copy link
Collaborator

AlbertSabate commented Sep 7, 2024

Given the scenario:

import { type RequestContext, navigate } from 'brisa';
import { rerenderInAction } from 'brisa/server';

export default function Page(_, { store }: RequestContext) {
if (renderInitiator === 'INITIAL_REQUEST') {
  store.set('modal', false);
  store.transferToClient(['modal']);
}

return (<>
<div
  onClick={() => {
    store.set('modal', true);
    rerenderInAction({ type: 'page' });
  }}
>
  Open Modal
</div>

<modal-dialog
  title="lala"
  isOpen={store.get('modal')}
>
  <div>
            {i18n.locales.map((_, i) => (
              <button
                key={i}
                type="button"
                onClick={(e) => {
                  store.set('modal', false);
                  store.transferToClient(['modal']);
                  navigate(
                    `/lala`,
                  );
                }}
              >
                aaa
              </button>
            ))}
  </div>
</modal-dialog>
</>);
}

And

export default function ModalDialog(
  {
    children,
    isOpen,
    title,
  }: {
    children: JSX.Element;
    title: string;
    isOpen?: boolean;
  },
  { derived }: WebContext,
) {
  const opened = derived(() => isOpen ?? false);
  return (<dialog open={opened.value}>{children}</dialog>);
}

After do a navigation the modal can't be open again. This seems because derived does not receive the updated value, and after navigation always remains in true.

Note: Tested with brisa 0.0.199.
This issue is different than: #454, but thanks to this fix, now we know this other happens.

@AlbertSabate AlbertSabate added the bug Something isn't working label Sep 7, 2024
@aralroca
Copy link
Collaborator

aralroca commented Sep 7, 2024

Looks that the store entry is diferent: modal-locale instead of modal.

@AlbertSabate
Copy link
Collaborator Author

AlbertSabate commented Sep 7, 2024

No that is a typo simplifying the code for you. Real code is good. I fix the example code i wrote. Issue is there

@aralroca aralroca self-assigned this Sep 8, 2024
@aralroca
Copy link
Collaborator

aralroca commented Sep 12, 2024

I think the problem is that you are not transferring after navigation, I recommend to transfer in each render, probably this code make more sense:

if (!store.has('modal')) store.set('modal', false);
store.transferToClient(['modal']); // Transfer in each request

Instead of:

if (renderInitiator === 'INITIAL_REQUEST') {
  store.set('modal', false);
  store.transferToClient(['modal']);
}

Otherwise, only if the navigation has a redirect for something, the transfer is lost.

@AlbertSabate
Copy link
Collaborator Author

Tested again, seems to work now :/ Magic happened...

@AlbertSabate
Copy link
Collaborator Author

closing issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants