Sp-dev-docs: Confusing onInit firing in Field Customizers when switching between views

Created on 11 Jan 2019  路  10Comments  路  Source: SharePoint/sp-dev-docs

Category

  • [x] Question
  • [ ] Typo
  • [x] Bug
  • [ ] Additional article idea

Expected or Desired Behavior

Field Customizer's onInit is called every time when switching between views. And only one time for every switch.

Observed Behavior

Field Customizer's onInit is called only first time when switching to particular view. And it is called twice at that moment.

Steps to Reproduce

  1. yo @microsoft/sharepoint
  2. change onInit:
public onInit(): Promise<void> {
  console.log('onInit is called');
  return Promise.resolve();
}
  1. Create a list with 2 views
  2. Add a column to the list and to both views
  3. Apply Field Customizer to that column
  4. Open AllItems view -> onInit alert appeared
  5. Switch to the second view -> onInit alert appeared twice - I think it's a bug
  6. switch back to AllItems view -> onInit alert hasn't appeared - I would expect it to appear
  7. switch back to the second view -> again, no alert.

So, sometimes onInit is called twice, sometimes not called at all.
I definitely see that there is an issue somewhere in the engine.

spfx-extensions tracked working-on-it bug-confirmed

All 10 comments

Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.

When it's called twice, do you have just one item in the view, or two?

@andrewconnell I tested it in multiple scenarios: when list views have only one item and more (10+).
Moreover, both views have exact the same items.

So, the issue is not related to the number of items in the view.
As I understand onInit is called only once to initialize customizer and then onRenderCell is called for each cell/row in the view.

I also think that this issue (initial reason for the issue) can be related to #1242

Following up...

on Targeted Release tenant the behavior is a bit better: it is called once when first time switching between view, but still not called when switching back.

One more update.
Add the next code to the field customizer:

  1. Add field private id: number = 0
  2. in onInit:
if (!this.id) {
      this.id = Date.now();
    }
alert(`FC onInit ${this.id}`);
  1. in onRenderCell:
console.log(`onRenderCell ${this.id}`);

Now, navigate to the list view page on Targeted Release.
You'll see an alert and some number of records in console (base on number of rows) like that:

onRenderCell 1559349780676

Now, switch to other view.
onInit may be and may not fire. It looks like it depends on time before switching to other view.
If onInit has been fired - you'll see in log other value for id:

onRenderCell 1559349859835

And now if you go back to the initial view, you'll see that the instance from the last view (with id = 1559349859835) is cached.

So, it's really, really confusing.

This is a cut/paste of the related bug you opened. Hopefully it is reasonable to you.

Sorry for the delay - working with the dev who is fixing a variety of the list customizer related issues. The plan is that onInit will fire only when the component is initialized. If you switch back and forth between multiple lists, and the "fast nav" type of navigation occurs, the onInit will not fire if you return to the original list (or any list where onInit has fired). This is because the objects will be initialized once and be reused when you return to the list. However - switching between lists should fire onNavigate events, the context should always be correct when in the context of a given list, and the items selection change should fire when switching lists. Dispose should be called when the whole app is torn down.

Does that match with your requirements?

@patmill - yes! Thanks for the update.

@AJIXuMuK Does this still happen? I am only able to get onInit to fire once when I navigate from a list with FCs to another with FCs.

Hi @kemesfun!
This issue is not about switching between lists. But between views of the same list.

current behavior is a bit different than in initial post:
Open AllItems view -> onInit alert appeared
Switch to the second view -> onInit alert appeared - this has changed. Now it is fired only once.
switch back to AllItems view -> onInit alert hasn't appeared - I would expect it to appear
switch back to the second view -> again, no alert.

Was this page helpful?
0 / 5 - 0 ratings