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

[BUG] Components don't re-render on React State update #3144

Open
tobi1220 opened this issue Mar 22, 2024 · 0 comments
Open

[BUG] Components don't re-render on React State update #3144

tobi1220 opened this issue Mar 22, 2024 · 0 comments
Labels
bug Something isn't working Needs: Triage 🔍

Comments

@tobi1220
Copy link

tobi1220 commented Mar 22, 2024

Describe the bug
The components don't re-render when a React state that's passed as a prop is updated. This happens for the React components in @microsoft/mgt-react as well as for the Web components in @microsoft/mgt-components (I tested both in a React app).

To Reproduce
You can test this with, for example, the file-list. (see full code below) Steps to reproduce the behavior:

  1. Init a new React app with CRA like so: yarn create react-app my-app --template typescript
  2. Add a stateful value and its update function like so: const [query, setQuery] = useState("");
  3. Add a <FileList /> in your App.tsx
  4. Add a function to the itemClick event that sets the current fileListQuery to the id of the clicked folder like so: <FileList fileExtensions={[""]} disableOpenOnClick itemClick={(e: CustomEvent<DriveItem>) => {setQuery(`/me/drive/items/${e.detail.id}/children`);}} fileListQuery={query}></FileList>

Expected behavior
The state update should trigger a re-render of <App /> and, by doing so, should update the MGT component.

Environment:

  • OS: Windows
  • Browser: Chrome
  • Framework: React
  • Context: MGT Core
  • Version: 4.1.0
  • Provider: new Msal2Provider({clientId: "redacted", authority: "redacted", redirectUri: "redacted", scopes: ["files.read"]});

Full code:

import {
  Login,
  FileList,
} from '@microsoft/mgt-react';
import { DriveItem } from '@microsoft/microsoft-graph-types';
import { useState } from 'react';

const App = () => {
  const [query, setQuery] = useState("");

  return (
    <div className='App'>
      <Login />

      <FileList /*key={query}*/ fileExtensions={[""]} disableOpenOnClick itemClick={(e: CustomEvent<DriveItem>) => {setQuery(`/me/drive/items/${e.detail.id}/children`);}} fileListQuery={query}></FileList> 
    </div>
  );
}

export default App;

Workaround
Add a key prop that is set to the respective state value to force a re-render every time this state is updated (although this is not recommended by React). In this case: key={query} (uncomment this in the full code provided above).

Additional context:
As mentioned above, this bug applies not only to the React- but also the Web components. I set up a new React app, registered the web components in it manually and set an attribute to a stateful value to test this.

@tobi1220 tobi1220 added bug Something isn't working Needs: Triage 🔍 labels Mar 22, 2024
@tobi1220 tobi1220 changed the title [BUG] [CORE] Components don't re-render on React State update [BUG] Components don't re-render on React State update Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Needs: Triage 🔍
Projects
Status: Needs Triage 🔍
Development

No branches or pull requests

1 participant