question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pass toast options to toast render function

See original GitHub issue

🚀 Feature request

I’d like to have the render function passed to the useToast accept props passed to the toast(...) call

🧱 Problem Statement / Justification

As the Toast component cannot be themed (opened an issue about it previously–https://github.com/chakra-ui/chakra-ui/issues/2736), I’d like to create my own useToast hook which would wrap the useToast hook from ChakraUI and provide a render function to it to customise the toast components.

However, when I tried implementing it this way I found out that the render function doesn’t accept the props passed to the toast(...) call such as status, title or description which makes it impossible for me to create a custom, re-usable useToast hook.

✅ Proposed solution or API

I’d like to be able to do something like this

import { useToast as useChakraToast, Box, Text } from '@chakra-ui/react'

export const useToast = props => {
    return useChakraToast({
        render(props) {
            const { status, title, description } = props

            return (
              <Box>
                {status}: <Text fontWeight={700}>{title}</Text><Text color="#eee">{description}</Text>
              </Box>
            )
        },
        ...props,
    })
}

↩️ Alternatives

I’m aware of the fact the there is a plan to introduce a ToastProvider which will enable us to pass a custom Toast component into it.

However, I still think that theming the Toast component or creating a re-usable useToast component is a better approach.

It’s especially beneficial when you have multiple apps using the same set of (themed) Chakra components.

The ToastProvider solution adds the need for extra configuration for every application which uses the same Chakra components whereas using a custom useToast hook (or theming the toast component) don’t.

📝 Additional Information

N/A

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:12
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
Grsmtocommented, Sep 14, 2021

For anyone looking for a solution, you can workaround with something along these lines:

import {
  UseToastOptions,
  useToast as useChakraToast
} from '@chakra-ui/react'

export const useToast = () => {
  const toast = useChakraToast()

  const returnFunction = (
    options: UseToastOptions
  ) =>
    toast({
      position: 'top',
      duration: 2000,
      render: () => <Toast {...options} {...toast} />,
      ...options
    })

  return Object.assign(returnFunction, toast)
}
1reaction
Grsmtocommented, Sep 14, 2021

@sinchang I fixed the types after posting the answer. I just edited my post with the correct types (hopefully).

Read more comments on GitHub >

github_iconTop Results From Across the Web

toast | React-Toastify - GitHub Pages
Options Type Description toastId string | number Provide a custom id type string One of info, success, warning, error onOpen function Called when the notification appear...
Read more >
toast() API
Available toast options. You can provide ToastOptions as the second argument. They will overwrite all options received from <Toaster/> .
Read more >
Configuring options in React Toast component - Syncfusion
By default toast can be rendered in the document body, we can change the target position for toast rendering using target property.
Read more >
Using React-Toastify to style your toast messages
All toasts are positioned at the top right of your browser by default. This position can be changed by assigning a new position...
Read more >
Create custom toast components with React
Component and create a render function that returns a react element, ... Whatever position prop is passed into the toast component, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found