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

[useCurrentUser] data never arrives for a postponed usage #119

Open
TiagoJacobs opened this issue Oct 3, 2024 · 0 comments
Open

[useCurrentUser] data never arrives for a postponed usage #119

TiagoJacobs opened this issue Oct 3, 2024 · 0 comments
Assignees

Comments

@TiagoJacobs
Copy link
Member

Let's say we have two components, mounted at same time:

function SecondComponent (props:PluginProps):React.ReactElement<PluginProps> {
  const pluginApi: PluginApi = BbbPluginSdk.getPluginApi(props.pluginUuid);
  const currentUser = pluginApi.useCurrentUser();

  useEffect ( () => {
    console.log("User data in second component: ", JSON.stringify(currentUser?.data));
  }, [
    JSON.stringify(currentUser?.data)
  ]);

  return <></>;
}

export default function MainPluginComponent (props:PluginProps):React.ReactElement<PluginProps> {
  BbbPluginSdk.initialize(props.pluginUuid);
  const pluginApi: PluginApi = BbbPluginSdk.getPluginApi(props.pluginUuid);
  const currentUser = pluginApi.useCurrentUser();

  console.log("User data in first component: ", JSON.stringify(currentUser?.data));

  return <>
    <SecondComponent {...props} />
  </>;

Everything works great:
image


However, if the second component is mounted afterwards, like in below code:

function SecondComponent (props:PluginProps):React.ReactElement<PluginProps> {
  const pluginApi: PluginApi = BbbPluginSdk.getPluginApi(props.pluginUuid);
  const currentUser = pluginApi.useCurrentUser();

  useEffect ( () => {
    console.log("User data in second component: ", JSON.stringify(currentUser?.data));
  }, [
    JSON.stringify(currentUser?.data)
  ]);

  return <></>;
}

export default function MainPluginComponent (props:PluginProps):React.ReactElement<PluginProps> {
  BbbPluginSdk.initialize(props.pluginUuid);
  const pluginApi: PluginApi = BbbPluginSdk.getPluginApi(props.pluginUuid);
  const currentUser = pluginApi.useCurrentUser();
  const [displaySecondComponent, setDisplaySecondComponent] = useState(false);
  useEffect( () => {
    setTimeout( () => {
      setDisplaySecondComponent(true);
    }, 2000);
  }, []);

  useEffect ( () => {
    console.log("User data in first component: ", JSON.stringify(currentUser?.data));
  }, [
    JSON.stringify(currentUser?.data)
  ]);

  return <>
  { 
    displaySecondComponent ? 
    <SecondComponent {...props} />
    :null 
  }
  </>;
}

Data never arrives into the second component:
image

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

No branches or pull requests

2 participants