<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Angular custom visual in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/Angular-custom-visual/m-p/1216880#M24699</link>
    <description>&lt;P&gt;Hi @Anonymous&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;I haven't personally unpacked this partcular pattern until you asked, as it's just works for me, but having done so (and thanks very much for asking, as it was quite a fun exercise), my understanding is as follows:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;We have two objects available to us in a React component that we can use to manage it: &lt;FONT face="courier new,courier"&gt;props&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;state&lt;/FONT&gt;.&lt;/LI&gt;
&lt;LI&gt;&lt;A href="https://reactjs.org/docs/faq-state.html#what-is-the-difference-between-state-and-props" target="_self"&gt;&lt;FONT face="courier new,courier"&gt;props&lt;/FONT&gt; are passed in upon creation/re-render, whereas &lt;FONT face="courier new,courier"&gt;state&lt;/FONT&gt; is managed internally to the component&lt;/A&gt;.&lt;/LI&gt;
&lt;LI&gt;In this example, the React component is instantiated in the visual's &lt;FONT face="courier new,courier"&gt;constructor&lt;/FONT&gt; method, meaning that there is only a chance to set its &lt;FONT face="courier new,courier"&gt;props&lt;/FONT&gt; when the visual is added to the canvas or its page is opened.&lt;/LI&gt;
&lt;LI&gt;This will be done for efficiency, i.e. it makes sense to only create the "app"-level component once rather than re-render it each time the visual updates.&lt;/LI&gt;
&lt;LI&gt;However, this is too soon in the visual's lifecyle to be able to get the info we need from the &lt;FONT face="courier new,courier"&gt;update&lt;/FONT&gt; method, such as the &lt;FONT face="courier new,courier"&gt;dataView&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;viewport&lt;/FONT&gt; etc.&lt;/LI&gt;
&lt;LI&gt;It's also not really the "React way" to destroy and create elements on changes.&lt;/LI&gt;
&lt;LI&gt;So in this example, the component &lt;FONT face="courier new,courier"&gt;props&lt;/FONT&gt; are inaccessible in the visual's update method, so the only way to get objects into our component is to manipulate the &lt;FONT face="courier new,courier"&gt;state&lt;/FONT&gt; externally. Which we techincally should not be doing. But we have to.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Your posted code snippet doesn't include the overloaded React methods, so let's add them from the example so we can round this out:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;    private static updateCallback: (data: object) =&amp;gt; void = null;

    public static update(newState: State) {
        if(typeof ReactCircleCard.updateCallback === 'function'){
            ReactCircleCard.updateCallback(newState);
        }
    }

    public componentWillMount() {
        ReactCircleCard.updateCallback = (newState: State): void =&amp;gt; { this.setState(newState); };
    }

    public componentWillUnmount() {
        ReactCircleCard.updateCallback = null;
    }&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If we think about this in terms of component lifecycle, what's happening here is as follows:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;If called statically, the &lt;FONT face="courier new,courier"&gt;updateCallback&lt;/FONT&gt; method doesn't do anything if invoked, because the component will have no &lt;FONT face="courier new,courier"&gt;state&lt;/FONT&gt;.&lt;/LI&gt;
&lt;LI&gt;When the component is about to mount (i.e. when the visual &lt;FONT face="courier new,courier"&gt;constructor&lt;/FONT&gt; method calls it) and React invokes &lt;FONT face="courier new,courier"&gt;componentWillMount&lt;/FONT&gt;, this overloads the previously &lt;FONT face="courier new,courier"&gt;void&lt;/FONT&gt;/&lt;FONT face="courier new,courier"&gt;null&lt;/FONT&gt; &lt;FONT face="courier new,courier"&gt;updateCallback&lt;/FONT&gt; method to now trigger the component's &lt;FONT face="courier new,courier"&gt;setState&lt;/FONT&gt; method from within by assigning it the fat arrow function.&lt;/LI&gt;
&lt;LI&gt;The component's &lt;FONT face="courier new,courier"&gt;update&lt;/FONT&gt; function will now pass through the state we supply from our visual's update method into the overloaded &lt;FONT face="courier new,courier"&gt;updateCallback&lt;/FONT&gt; function and this will update the component state correctly. Similarly, if this function is called statically, it won't do anything as the updateCallback is&amp;nbsp;&lt;FONT face="courier new,courier"&gt;void&lt;/FONT&gt;/&lt;FONT face="courier new,courier"&gt;null&lt;/FONT&gt; at this point.&lt;/LI&gt;
&lt;LI&gt;When the visual is unloaded/destroyed, the &lt;FONT face="courier new,courier"&gt;componentWillUnmount&lt;/FONT&gt; method (triggered by React) will reset the overloaded &lt;FONT face="courier new,courier"&gt;updateCallback&lt;/FONT&gt; function to &lt;FONT face="courier new,courier"&gt;null&lt;/FONT&gt; again.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;If you want to unpack this further or get an explanation for the rationale, it'll be best to follow up with the team on the previously supplied email and hopefully they can clarify further. Please share if you find anything out &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;HR /&gt;
&lt;P&gt;One thing I have found with using this in the real world is, if you need to add properties to the component &lt;FONT face="courier new,courier"&gt;State&lt;/FONT&gt; interface that are not part of the visual's &lt;FONT face="courier new,courier"&gt;update&lt;/FONT&gt; method, it's handy to modify the &lt;FONT face="courier new,courier"&gt;update&lt;/FONT&gt; function's signature to accept &lt;FONT face="courier new,courier"&gt;&amp;lt;Partial&amp;gt;State&lt;/FONT&gt; as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;    public static update(newState: &amp;lt;Partial&amp;gt;State) {
        /** function code as before */
    }&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The alternative is to declare properties that aren't available to be supplied from the visual's update method as optional (with a &lt;FONT face="courier new,courier"&gt;?&lt;/FONT&gt;) in the State interface, e.g.:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;export interface State {
    textLabel: string,
    textValue: string,
    internalStateValue?: string
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The latter way adds the overhead of having to check each optional property whenever you need to use it, so I prefer the former as I at least know that my initial &lt;FONT face="courier new,courier"&gt;state&lt;/FONT&gt; is always going to be correct when the visual instantiates.&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;Daniel&lt;/P&gt;</description>
    <pubDate>Sun, 12 Jul 2020 21:56:41 GMT</pubDate>
    <dc:creator>dm-p</dc:creator>
    <dc:date>2020-07-12T21:56:41Z</dc:date>
    <item>
      <title>Angular custom visual</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Angular-custom-visual/m-p/1214588#M24678</link>
      <description>&lt;P&gt;Can we use angular 8+ to create custom visulas. I could see links for creating custom visual in React. Similarly can we create a custom visual in angular&lt;/P&gt;</description>
      <pubDate>Fri, 10 Jul 2020 12:05:06 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Angular-custom-visual/m-p/1214588#M24678</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-07-10T12:05:06Z</dc:date>
    </item>
    <item>
      <title>Re: Angular custom visual</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Angular-custom-visual/m-p/1215807#M24691</link>
      <description>&lt;P&gt;Hi @Anonymous&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;I'm more of a React person, so haven't used Angular beyond auditioning it some years back, but as you can install Angular via npm and it uses the latest ES6 imports, you will be able to use it in custom visuals. You would need to ensure that you can fit it into the current &lt;FONT face="courier new,courier"&gt;visual.ts&lt;/FONT&gt; entry points, but this is basically how custom visuals with React are done and the example you've found is documented:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;FONT face="courier new,courier"&gt;constructor&lt;/FONT&gt; instantiates DOM&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face="courier new,courier"&gt;update&lt;/FONT&gt; handles changes to the components&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;I suspect you would also need to &lt;A href="https://angular.io/guide/typescript-configuration" target="_self"&gt;set up your tsconfig&lt;/A&gt; appropriately, and possibly Webpack depending on how you need to bundle your project up.&lt;/P&gt;
&lt;P&gt;I've adopted a similar approach with writing visuals for clients that essentially wrap other charting libraries such as Vega, Highcharts and amCharts - while these aren't as fully-featured as React/Angular, you use the visual.ts methods to manage the brokering of data from your model to the rest of your code - &lt;A href="https://github.com/microsoft/vegalite-for-powerbi" target="_self"&gt;here's a shareable example using Vega&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Your best bet might be to contact the team - &lt;A href="mailto:pbicvsupport@microsoft.com" target="_blank"&gt;pbicvsupport@microsoft.com&lt;/A&gt; - to see if there are any specific things to consider with this approach for Angular.&lt;/P&gt;
&lt;P&gt;Good luck!&lt;/P&gt;
&lt;P&gt;Daniel&lt;/P&gt;</description>
      <pubDate>Sat, 11 Jul 2020 02:37:25 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Angular-custom-visual/m-p/1215807#M24691</guid>
      <dc:creator>dm-p</dc:creator>
      <dc:date>2020-07-11T02:37:25Z</dc:date>
    </item>
    <item>
      <title>Re: Angular custom visual</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Angular-custom-visual/m-p/1216457#M24696</link>
      <description>&lt;P&gt;Thats great thanks I will try using react as It has a simpler setup without webpacks and tsconfig. I looked into the titorial and came accross this line of code which I am trying to understand. It would be helpfull if you could explain it&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;    private static updateCallback: (data: object) =&amp;gt; void = null;

    public static update(newState: State) {
        if(typeof ReactCircleCard.updateCallback === 'function'){
            ReactCircleCard.updateCallback(newState);
        }
    }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;We have declared updateCallback and checked if it is a function type inside update method. may I know why ?&lt;/P&gt;</description>
      <pubDate>Sun, 12 Jul 2020 06:10:56 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Angular-custom-visual/m-p/1216457#M24696</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-07-12T06:10:56Z</dc:date>
    </item>
    <item>
      <title>Re: Angular custom visual</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Angular-custom-visual/m-p/1216880#M24699</link>
      <description>&lt;P&gt;Hi @Anonymous&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;I haven't personally unpacked this partcular pattern until you asked, as it's just works for me, but having done so (and thanks very much for asking, as it was quite a fun exercise), my understanding is as follows:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;We have two objects available to us in a React component that we can use to manage it: &lt;FONT face="courier new,courier"&gt;props&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;state&lt;/FONT&gt;.&lt;/LI&gt;
&lt;LI&gt;&lt;A href="https://reactjs.org/docs/faq-state.html#what-is-the-difference-between-state-and-props" target="_self"&gt;&lt;FONT face="courier new,courier"&gt;props&lt;/FONT&gt; are passed in upon creation/re-render, whereas &lt;FONT face="courier new,courier"&gt;state&lt;/FONT&gt; is managed internally to the component&lt;/A&gt;.&lt;/LI&gt;
&lt;LI&gt;In this example, the React component is instantiated in the visual's &lt;FONT face="courier new,courier"&gt;constructor&lt;/FONT&gt; method, meaning that there is only a chance to set its &lt;FONT face="courier new,courier"&gt;props&lt;/FONT&gt; when the visual is added to the canvas or its page is opened.&lt;/LI&gt;
&lt;LI&gt;This will be done for efficiency, i.e. it makes sense to only create the "app"-level component once rather than re-render it each time the visual updates.&lt;/LI&gt;
&lt;LI&gt;However, this is too soon in the visual's lifecyle to be able to get the info we need from the &lt;FONT face="courier new,courier"&gt;update&lt;/FONT&gt; method, such as the &lt;FONT face="courier new,courier"&gt;dataView&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;viewport&lt;/FONT&gt; etc.&lt;/LI&gt;
&lt;LI&gt;It's also not really the "React way" to destroy and create elements on changes.&lt;/LI&gt;
&lt;LI&gt;So in this example, the component &lt;FONT face="courier new,courier"&gt;props&lt;/FONT&gt; are inaccessible in the visual's update method, so the only way to get objects into our component is to manipulate the &lt;FONT face="courier new,courier"&gt;state&lt;/FONT&gt; externally. Which we techincally should not be doing. But we have to.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Your posted code snippet doesn't include the overloaded React methods, so let's add them from the example so we can round this out:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;    private static updateCallback: (data: object) =&amp;gt; void = null;

    public static update(newState: State) {
        if(typeof ReactCircleCard.updateCallback === 'function'){
            ReactCircleCard.updateCallback(newState);
        }
    }

    public componentWillMount() {
        ReactCircleCard.updateCallback = (newState: State): void =&amp;gt; { this.setState(newState); };
    }

    public componentWillUnmount() {
        ReactCircleCard.updateCallback = null;
    }&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If we think about this in terms of component lifecycle, what's happening here is as follows:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;If called statically, the &lt;FONT face="courier new,courier"&gt;updateCallback&lt;/FONT&gt; method doesn't do anything if invoked, because the component will have no &lt;FONT face="courier new,courier"&gt;state&lt;/FONT&gt;.&lt;/LI&gt;
&lt;LI&gt;When the component is about to mount (i.e. when the visual &lt;FONT face="courier new,courier"&gt;constructor&lt;/FONT&gt; method calls it) and React invokes &lt;FONT face="courier new,courier"&gt;componentWillMount&lt;/FONT&gt;, this overloads the previously &lt;FONT face="courier new,courier"&gt;void&lt;/FONT&gt;/&lt;FONT face="courier new,courier"&gt;null&lt;/FONT&gt; &lt;FONT face="courier new,courier"&gt;updateCallback&lt;/FONT&gt; method to now trigger the component's &lt;FONT face="courier new,courier"&gt;setState&lt;/FONT&gt; method from within by assigning it the fat arrow function.&lt;/LI&gt;
&lt;LI&gt;The component's &lt;FONT face="courier new,courier"&gt;update&lt;/FONT&gt; function will now pass through the state we supply from our visual's update method into the overloaded &lt;FONT face="courier new,courier"&gt;updateCallback&lt;/FONT&gt; function and this will update the component state correctly. Similarly, if this function is called statically, it won't do anything as the updateCallback is&amp;nbsp;&lt;FONT face="courier new,courier"&gt;void&lt;/FONT&gt;/&lt;FONT face="courier new,courier"&gt;null&lt;/FONT&gt; at this point.&lt;/LI&gt;
&lt;LI&gt;When the visual is unloaded/destroyed, the &lt;FONT face="courier new,courier"&gt;componentWillUnmount&lt;/FONT&gt; method (triggered by React) will reset the overloaded &lt;FONT face="courier new,courier"&gt;updateCallback&lt;/FONT&gt; function to &lt;FONT face="courier new,courier"&gt;null&lt;/FONT&gt; again.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;If you want to unpack this further or get an explanation for the rationale, it'll be best to follow up with the team on the previously supplied email and hopefully they can clarify further. Please share if you find anything out &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;HR /&gt;
&lt;P&gt;One thing I have found with using this in the real world is, if you need to add properties to the component &lt;FONT face="courier new,courier"&gt;State&lt;/FONT&gt; interface that are not part of the visual's &lt;FONT face="courier new,courier"&gt;update&lt;/FONT&gt; method, it's handy to modify the &lt;FONT face="courier new,courier"&gt;update&lt;/FONT&gt; function's signature to accept &lt;FONT face="courier new,courier"&gt;&amp;lt;Partial&amp;gt;State&lt;/FONT&gt; as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;    public static update(newState: &amp;lt;Partial&amp;gt;State) {
        /** function code as before */
    }&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The alternative is to declare properties that aren't available to be supplied from the visual's update method as optional (with a &lt;FONT face="courier new,courier"&gt;?&lt;/FONT&gt;) in the State interface, e.g.:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;export interface State {
    textLabel: string,
    textValue: string,
    internalStateValue?: string
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The latter way adds the overhead of having to check each optional property whenever you need to use it, so I prefer the former as I at least know that my initial &lt;FONT face="courier new,courier"&gt;state&lt;/FONT&gt; is always going to be correct when the visual instantiates.&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;Daniel&lt;/P&gt;</description>
      <pubDate>Sun, 12 Jul 2020 21:56:41 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Angular-custom-visual/m-p/1216880#M24699</guid>
      <dc:creator>dm-p</dc:creator>
      <dc:date>2020-07-12T21:56:41Z</dc:date>
    </item>
    <item>
      <title>Re: Angular custom visual</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Angular-custom-visual/m-p/1217244#M24701</link>
      <description>&lt;P&gt;Thanks for the detailed explanation &lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/397"&gt;@dm-p&lt;/a&gt;&amp;nbsp;. It was nice to know the reason for the updateCallback implementation.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jul 2020 10:44:06 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Angular-custom-visual/m-p/1217244#M24701</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-07-16T10:44:06Z</dc:date>
    </item>
  </channel>
</rss>

