SPFX fabric webpart works differently in Modern page than in Classic Page
Thank you for reporting an issue or suggesting an enhancement. We appreciate your feedback - to help the team to understand your needs, please complete the below template to ensure we have the necessary details to assist you.
Category
- [ ] Question
- [ ] Typo
- [x] Bug
- [ ] Enhancement / Suggestion
- [ ] Additional article idea
Expected or Desired Behavior
When I create a n spfx webpart that uses an office UI Fabric command bar that has Items with sub-items, the page should not reload when I click on the parent item in both modern and classic pages.
Observed Behavior
I have the same webpart deployed to both a classic and modern page. On a Modern page, when I click on the main menu item it shows the dropdown with sub menu items. I can click on the sub menu items and the onClick is run.
On a Classic page with the same webpart, when I click on the main menu item it shows the dropdown with sub menu items,, then the page refreshes.
Steps to Reproduce
Deploy the webpart at https://github.com/russgove/issue to both a modern an a classic page to see the difference.
More info on stack exchange question at http://sharepoint.stackexchange.com/questions/210644/spfx-fabric-webpart-works-differently-in-modern-page-than-in-classic-page
@manishgarg1 @srideshpande ?
Hi @russgove ,
this is a known issue with classic pages. Every button click causes a postback in classic pages. We have some utility code to prevent that but it is still not public and under test.
In the mean time you can try to run the following code where the domElement is any div inside which you want to disable postbacks and see if that helps. But please beware, this is test code for now and may change going forward.
if (env === EnvironmentType.ClassicSharePoint) {
const buttons: NodeListOf
Hi @russgove, were you able to try the work around? Did it work for you?
Hi, It does work. I added this code in the render method of my spfx react webpart:
this.reactElement = React.createElement(TrForm, formProps);
var formComponent: TrForm = ReactDom.render(this.reactElement, this.domElement) as TrForm;//render the component
if (Environment.type === EnvironmentType.ClassicSharePoint) {
const buttons: NodeListOf
Hi @russgove , good to know that it works. We have also made some changes to the internal web part framework. You could try removing your code and see if the behavior persists. If yes, you can keep this code around. Not ideal but works.
Thanx for your feedback. Considering your issue is resolved, I am closing the issue at this time. Please feel free to reactive the issue if the problem persists.
I see this issue as well -- clicking buttons seems to refresh the page in classic. Perhaps there is a workaround, but this a hard issue to track-down and patch.. perhaps we should leave this open until it's addressed in its core?
I get this issue when I load my webpart on a Project Detail Page for Project Server Online. So, I guess it's still a current issue. The code russgove posted really helped me out. Except my controls change, so I had to make it happen after each render. I did it by over-rideing methods as follows:
componentDidUpdate?(prevProps: Readonly<P>, prevState: Readonly<S>, prevContext: any): void {
this.removePostbacksForClassicPages();
}
componentDidMount():void {
this.removePostbacksForClassicPages();
}
removePostbacksForClassicPages() {
// The pages always post back on classic pages, and this script removes the calls to the post backs.
// if (Environment.type === EnvironmentType.ClassicSharePoint) {
/* tslint:disable */
//React.ReactDOM.findDOMNode<HTMLInputElement>(this);
var thisDomElement = ReactDOM.findDOMNode(this);
/* tslint:enable */
const buttons = thisDomElement.getElementsByTagName('button');
if (buttons && buttons.length) {
for (let i: number = 0; i < buttons.length; i++) {
if (buttons[i]) {
/* tslint:disable */
// Disable the button onclick postback
buttons[i].onclick = function () { return false; };
/* tslint:enable */
}
}
// }
}
}
Note, I commented out the line if (Environment.type === EnvironmentType.ClassicSharePoint) ... as this causes an error somewhere deep in my imported sharepoint libraries.
Hi, since there is a work around, I am resolving this issue. In future we may ship that API as public API in SPFx.
i,don't think this issue should be closed. i understand from @VesaJuvonen that engineering is working on a fix.
is the issue , and the workaround , documented anywhere. this is going to frustrate a lot of deva.
Hi @russgove, thanx for the feedback. Let me follow up and make sure the issue is well documented and if I can accelerate shipping some code fix. I have one more request. Can you log the issue on http://aka.ms/sp-dev-uservoice also. That will help us prioritize the issue.
Hi all,
I came across this post while having the same issue. For those who might be struggling with this, adding the prop type: 'button' to the CommandBar item will solve the postback on classic pages.
Like this:
private getItems = () => { return [ { key: 'newItem', name: 'New', iconProps: { iconName: 'Add' }, type: 'button' } ] }
Issues that have been closed & had no follow-up activity for at least 7 days are automatically locked. Please refer to our wiki for more details, including how to remediate this action if you feel this was done prematurely or in error: Issue List: Our approach to locked issues