Forum Discussion

marrocksd's avatar
marrocksd
Regular Visitor
5 years ago
Solved

Power BI Custom Visual state persistence

Hi all,

 

I am building a custom table, so basically my table has some expandable/collapsible rows.

 

I am using React, so whenever I filter for new data, the expanded rows remain. That's great, expected behavior.

 

However, when I go to another page and come back, the visual re-initializes, which means I lose all the current states.

With default table/matrix, I don't encounter such problems.

 

Is there anyway I can persist those states

I know there is a thing called persistProperties, but AFAIK it persists "Format Pane" only.

Thank you all.

 

  • Hi marrocksd

    persistProperties is indeed the right way to do this, as React state is lost when the visual is destroyed. The only way to store anything you want persisted in a visual for when it's next instantiated is to use the objects/properties, as the developer visual sandbox prevents cookies and local storage. You would restore the property into your visual state from here when the visual's update method runs and settings are enumerated.

    A property doesn't have to be visible in the pane, but does have to be declared in your capabilities.json and available to your VisualSettings. However, you can remove that property from the formatting pane during enumerateObjectInstances (or, if you are manually pushing in the objects, like the sample bar chart, you can just omit it) so that the user doesn't see it. It will still be present in your visual's objects as a persistable property.

    If your property is a complex object that doesn't fit the data types that capabilities.json offers, you can declare a text property in your capabilities and stringify the JSON for storage (and then parse it when settings are enumerated).

    Regards,

    Daniel

3 Replies

  • dm-p's avatar
    dm-p
    Super User

    Hi marrocksd

    persistProperties is indeed the right way to do this, as React state is lost when the visual is destroyed. The only way to store anything you want persisted in a visual for when it's next instantiated is to use the objects/properties, as the developer visual sandbox prevents cookies and local storage. You would restore the property into your visual state from here when the visual's update method runs and settings are enumerated.

    A property doesn't have to be visible in the pane, but does have to be declared in your capabilities.json and available to your VisualSettings. However, you can remove that property from the formatting pane during enumerateObjectInstances (or, if you are manually pushing in the objects, like the sample bar chart, you can just omit it) so that the user doesn't see it. It will still be present in your visual's objects as a persistable property.

    If your property is a complex object that doesn't fit the data types that capabilities.json offers, you can declare a text property in your capabilities and stringify the JSON for storage (and then parse it when settings are enumerated).

    Regards,

    Daniel

  • marrocksd's avatar
    marrocksd
    Regular Visitor

    Thanks alot dm-p . I'll try that right now but I believe that's the right way.

     

    I have been using useState hook. So it would be easy to add a listener to save and persist all states, then load them as init states next time.

  • marrocksd's avatar
    marrocksd
    Regular Visitor

    Hi dm-p , one other thing. It's about when to persist the state.

    Currently I'm persisting all states everytime the user expands or collapses a row... sound not good for performance at all.

    I've tried:

     - public destroy() method in Visual class

     - public componentWillUnmount method in Visual class

     - React useEffect(... return...) in my component

    None works, could you help me please, thanks a ton