How to use react-flexmonster hooks and ref

Answered
Tiago asked on May 3, 2022

Hello,
 
I'm trying to use ref in my FlexmonsterPivot but it seems like the parameter doesn't exist on Flexmonster.Params.
 
Heres my code:

import Flexmonster from 'flexmonster';

import dynamic from 'next/dynamic';
 

const FlexmonsterPivot = dynamic<Flexmonster.Params>(
  () => import('react-flexmonster/hooks').then((m) => m.Pivot),
  { ssr: false }
);
 

<FlexmonsterPivot
   toolbar={true}
   componentFolder="https://cdn.flexmonster.com/"
   report={report}
   ...
/>

 
In the attached photo you can see that the parameter ref is missing.
 

Kind regards,
Tiago

Attachments:
react-flexmonster.png

10 answers

Public
Maksym Diachenko Maksym Diachenko Flexmonster May 4, 2022

Hello, Tiago!

Thank you for your question.

Please follow these steps for creating and using refs with FlexmonsterPivot:

Using react-flexmonster
You should change the import statement from  import Flexmonster from 'flexmonster' to import * as FlexmonsterReact from 'react-flexmonster'. It will provide you with a ready-to-use package with React wrapper of Flexmonster JS library.
To install react-flexmonster node module, use the following command: 

npm i react-flexmonster

For more information, please see the integration with React tutorial page
Also, check or example React + Typescript project with Flexmonster. It contains examples with different use-cases of Flexmonster, including creating & using refs (see UpdatingData.tsx on GitHub). To download and run the project, use this Flexmonster CLI command: 

flexmonster create react typescript -r

Creating ref
To create the ref, you can use the following code inside the React class, using Flexmonster (UpdatingData.tsx, line 7):

private pivotRef: React.RefObject<FlexmonsterReact.Pivot> = React.createRef<FlexmonsterReact.Pivot>();

Use the following code to specify the ref attribute inside the FlexmonsterReact.Pivot component (UpdatingData.tsx, line 84):

ref={this.pivotRef}

Using ref
After following these steps, Flexmosnter API calls can be done from this.pivotRef.current!.flexmonster.

Hope you will find this information helpful.

Best Regards,
Maksym

Public
Tiago May 4, 2022

Hello, Maksym!
 
When i use import * as FlexmonsterReact from 'react-flexmonster' it works like a charm, but i cannot use it since whenever i try to reload the page it crashes, missing the window reference, so i gotta use this import while using dynamic from nextjs.
 
const FlexmonsterPivot = dynamic<Flexmonster.Params>(
  () => import(‘react-flexmonster’).then((m) => m.Pivot),
  { ssr: false }
);
 
Best Regards,
Tiago

Public
Maksym Diachenko Maksym Diachenko Flexmonster May 5, 2022

Hi, Tiago!

We are glad to hear that the solution with dynamic import works for your project. You are welcome to reach out to us if further questions arise.

Best Regards,
Maksym

Public
Tiago May 5, 2022

Hello, Maksym!
 
Sorry for the misunderstood but i still haven't found a solution, this import i make allows me to refresh the page, but in the code, using the FlexmonsterPivot, the ref parameter continues to be missing since the Flexmonster.Params doensn't have one (you can see the code in the first attachment i sent you)
 
PS: I'm using Nextjs with typescript.
 
Best Regards,
Tiago

Public
Maksym Diachenko Maksym Diachenko Flexmonster May 6, 2022

Hello, Tiago!

Our apologies for the misunderstanding.

We would like to offer a working approach for using react-flexmonster refs in a Next.js project.

The idea behind this solution is:

  • Firstly, you should create a separate React component, containing FlexmonterReact.Pivot. Here you can configure Flexmonster and use refs as described earlier in this thread.
  • Later, this component can be used in your NextPage. To prevent the app from crashing with `Reference error: window is not defined` on page reload, use dynamic import for the previously created component.
    const PivotTableDemo = dynamic<Flexmonster.Params>(
      () => import('./PivotTableDemo').then((m) => m),
      { ssr: false }
    );

You are welcome to check out a sample project where we tested this approach (see "fm-next-ts.zip" in the attachments). Use the following commands to run the project:

npm install
npm run dev

Please let us know if such an approach would work for your case. Looking forward to your response.

Best Regards,
Maksym

Public
Maksym Diachenko Maksym Diachenko Flexmonster May 6, 2022

Attachments:

Attachments:
fm-next-ts.zip

Public
Tiago May 6, 2022

Hello, Maksym!
 
Thanks for the sample, it works like a charm, but how can I pass parameters to the component? I tried a few ways and they all ended up giving me an error on the import. You can check the error in the attachements.
 
Best Regards,
Tiago

Attachments:
import_parameters.png

Public
Maksym Diachenko Maksym Diachenko Flexmonster May 10, 2022

Hello, Tiago!

Hope you are doing well.

We modified the sample project in the way that Flexmosnter is able to receive props outside the PivotTableDemo class. We suggest following these steps to achieve this functionality:

  • Change the <FlexmonsterReact.Pivot> inside PivotTableDemo.js, so it retrieves all the props from outside, using the ... operator:
                        <FlexmonsterReact.Pivot
                            ref={this.pivotRef}
                            {...this.props}
                        />
  • Now you will be able to pass the Flexmonster params from the NextPage:
          <PivotTableDemo
            toolbar={false}
            report={"https://cdn.flexmonster.com/github/demo-report.json"}
            //...other Flexmonster parameters
          />

You are welcome to check the modified sample project in the attachments.

Please let us know if everything works fine.

Best Regards,
Maksym

Attachments:
fm-next-ts.zip

Public
Tiago May 12, 2022

Hello, Maksym!
 
I actually solved this issue by changing the expected type params from the import, heres the code:

const FlexmonsterPivot = dynamic<MyExpectedParams>(
  () => import('@/mySrc/FlexmonsterPivot').then((m) => m),
  { ssr: false }
);

 
Best Regards,
Tiago

Public
Maksym Diachenko Maksym Diachenko Flexmonster May 12, 2022

Hi, Tiago!

Thank you for your feedback.
We are happy to hear that you found a working solution.
Feel free to contact us if you have more questions.

Best Regards,
Maksym

Please login or Register to Submit Answer