Error toast is not defined

UI Components for React
rizarefaldi
Posts: 1
Joined: 02 Jul 2021, 16:02

02 Jul 2021, 16:06

Hello, i am adding next lines:

Code: Select all

import { Toast } from 'primereact/toast';

<Toast ref={toast} />
and ukuran 4x6

Code: Select all

toast.current.show({severity: 'success', summary: 'Success Message', detail: 'Order submitted'});
in a function that i want show the toast but i am getting this error: 'toast' is not defined'

am i forgetting something?

mert.sincan
Posts: 5281
Joined: 29 Jun 2013, 12:38

05 Jul 2021, 08:18

Hi,
Interesting! It works fine for me. Please see our demo;
https://codesandbox.io/s/grp5c

Best Regards,

mcandu
Posts: 107
Joined: 20 Jan 2021, 16:31

06 Jul 2021, 09:21

Did you add,

Code: Select all

const toast = useRef(null);

TheVoid
Posts: 4
Joined: 12 May 2017, 09:30

30 Jun 2022, 11:43

Hi,
I too run in a problem with the toast example.
I create a new react with typescript project => npx create-react-app efeu-portal-prime --template typescript and added the code below.
The error is
ERROR in src/App.tsx:18:24
TS2339: Property 'show' does not exist on type 'never'.
Any hints?

Regards
Jürgen

Code: Select all

import React, { useRef, useState } from 'react';
import logo from './logo.svg';
import './App.css';

import { InputText } from "primereact/inputtext";
import { Button } from 'primereact/button';
import { Toast } from 'primereact/toast';

import "primereact/resources/themes/lara-light-indigo/theme.css";  //theme
import "primereact/resources/primereact.min.css";                  //core css
import "primeicons/primeicons.css";                                //icons

function App() {
  const [text, setText] = useState("Hallo");
  const toastRef = useRef(null);

  const showToast = (severityValue: string, summaryValue: string, detailValue: string) => {

    toastRef?.current?.show({ severity: severityValue, summary: summaryValue, detail: detailValue });
  }

  const onButtonClick = () => {

    if (text) {
      showToast("severityValue", "summaryValue", "detailValue");
    }
  }

  return (
    <div className="App">
      <Toast ref={toastRef}></Toast>
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <InputText value={text} onChange={e => setText(e.target.value)}></InputText>
        {text}
        <Button type='button' label='Save' icon="pi pi-check" onClick={onButtonClick}></Button>
      </header>
    </div>
  );
}

export default App;

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

30 Jun 2022, 13:55

Please put together a Code Sandbox reproducer showing the issue using my starter PrimeReact template: https://codesandbox.io/s/primereact-test-forked-bbns8k
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

vekanoid
Posts: 4
Joined: 08 Aug 2022, 14:34

08 Aug 2022, 14:39

TheVoid wrote:
30 Jun 2022, 11:43
TS2339: Property 'show' does not exist on type 'never'.
I have the same problem.
Looks like Toast methods are not visible from toast.current.
Maybe that is because I am using react with Typescript in combination or there is some bug in primereact lib?

vekanoid
Posts: 4
Joined: 08 Aug 2022, 14:34

08 Aug 2022, 14:43

Melloware wrote:
30 Jun 2022, 13:55
Please put together a Code Sandbox reproducer showing the issue using my starter PrimeReact template: https://codesandbox.io/s/primereact-test-forked-bbns8k
I tried from the template and it works fine there, but in my project I have problem with show method like mentioned above.
Can you try with React project with typescript instead of javascript?

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

09 Aug 2022, 14:09

Sorry use my Vite stackblitz its all TypeScript using PrimeReact similar to Code Sandbox.

https://stackblitz.com/edit/vitejs-vite ... ckage.json
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

vekanoid
Posts: 4
Joined: 08 Aug 2022, 14:34

09 Aug 2022, 15:37

Melloware wrote:
09 Aug 2022, 14:09
Sorry use my Vite stackblitz its all TypeScript using PrimeReact similar to Code Sandbox.

https://stackblitz.com/edit/vitejs-vite ... ckage.json
Thank you Melloware, I found what is problem. In your example from the link I see toast const is defined like this:

Code: Select all

const toast = useRef<Toast>(null);
And with that everything works fine. If I use example from primereact documentation https://www.primefaces.org/primereact/toast/

Code: Select all

const toast = useRef(null);

That will not work with typescript.
In your documentation (TS Source) there is no defined type for useRef and that is the reason why show method is not visible and why people have problem.
Please update documentation for primereact Toast section.

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

09 Aug 2022, 15:49

Oh yes all the Typescript examples are not correct see: https://github.com/primefaces/primereact/issues/2490
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 9 guests