What SharePoint development model, framework, SDK or API is this about?
💥 SharePoint Framework
Developer environment
Windows
What browser(s) / client(s) have you tested
[ ] 💥 Internet Explorer
[ ] 💥 Microsoft Edge
[X] 💥 Google Chrome
[ ] 💥 FireFox
[ ] 💥 Safari
[ ] mobile (iOS/iPadOS)
[ ] mobile (Android)
[ ] not applicable
[ ] other (enter in the "Additional environment details" area below)
Additional environment details
browser version: Chrome 126.0.6478.127
SPFx version: 1.18.2
Node.js version: 18.19
Describe the bug / error
The base component in a a web part will when moving from 'view mode' to 'edit mode' mount twice like this:
Clicking the page 'Edit' control
Component dismounts
Component mounts
Component dismounts
Component mounts
This might be by design (I haven't found any information about it when searching) but could otherwise be a bug.
The componentDidMount (or useEffect with no dependencies) are in many cases used to run requests/fetching data necessary for the initialization of a component. When the component is mounted twice such requests that are indended as a one of fetch will be made twice
Steps to reproduce
I created a very simple web part with some minimal console logging to illustrate the issue. Scaffolded through yo @microsoft/sharepoint. Available here: https://github.com/JoelBool/DoubleMountingWebpart
Package the web part and deploy it to a app catalog (npm run package)
Edit a page
Add the webpart to the page (it has the name re-mounting-re-rendering)
Publish the page
Open the developer tools of the browser to see the console.logs
Clear the logs that are in the console (so it's easier to spot the coming ones)
Edit the page
Note that 'ComponentDidMount' is logged twice in the console
The issue does not occur in view mode. Nor when adding a web part when already in edit mode.
Expected behavior
Expecting componendDidMount (or useEffect with no dependencies) to run once when editing a page.
Target SharePoint environment
SharePoint Online
What SharePoint development model, framework, SDK or API is this about?
💥 SharePoint Framework
Developer environment
Windows
What browser(s) / client(s) have you tested
Additional environment details
Describe the bug / error
The base component in a a web part will when moving from 'view mode' to 'edit mode' mount twice like this:
This might be by design (I haven't found any information about it when searching) but could otherwise be a bug. The componentDidMount (or useEffect with no dependencies) are in many cases used to run requests/fetching data necessary for the initialization of a component. When the component is mounted twice such requests that are indended as a one of fetch will be made twice
Steps to reproduce
re-mounting-re-rendering
)The issue does not occur in view mode. Nor when adding a web part when already in edit mode.
Expected behavior
Expecting componendDidMount (or useEffect with no dependencies) to run once when editing a page.