Toast in class service

UI Components for React
Post Reply
gokhantiknaz
Posts: 10
Joined: 11 Nov 2020, 15:19

28 Jan 2022, 10:19

Hello. I just try to show toast message in Service class.

Like ,
nodeService.getTreeNodes().then(data => setNodes(data));

and if i got error in class method i want to show toaster with error message. could i do that?

getTreeTableNodes() {
return fetch('data/treetablenodes.json').then(res => res.json())
.then(d => d.root).catch(error= { toaster.show("error message")});
}

Melloware
Posts: 3716
Joined: 22 Apr 2013, 15:48

30 Jan 2022, 18:49

You have to have a REF to your Toaster and I don't see that in your code above.
PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

gokhantiknaz
Posts: 10
Joined: 11 Nov 2020, 15:19

07 Feb 2022, 09:17

Thanks for reply.

here ise sandbox

https://codesandbox.io/s/loving-chatter ... Service.js
Line : 14

Melloware
Posts: 3716
Joined: 22 Apr 2013, 15:48

07 Feb 2022, 14:59

YEah you will have to pass a ref to your TOAST into your service method like...

Code: Select all

getProduct(toast: Toast) {...
PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

gokhantiknaz
Posts: 10
Joined: 11 Nov 2020, 15:19

07 Feb 2022, 15:33

that worked. Thank you.

So can I access the context inside the service class? i dont want to pass toast parameter in all methods.
i put my toast ref to context; like : <AuthContext.Provider value={{toast,

Melloware
Posts: 3716
Joined: 22 Apr 2013, 15:48

07 Feb 2022, 15:57

Yep that would be your other option. You just need some reference to it.
PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

Post Reply

Return to “PrimeReact”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 2 guests