jeanverster / react-native-styled-toast

A theme friendly, easy to use react-native toast component built using styled-components and styled-system.
MIT License
315 stars 33 forks source link

Trigger Toast outside of react component. #54

Open HridayK97 opened 3 years ago

HridayK97 commented 3 years ago

Is there a way to trigger the toast outside of the react component/jsx. I need a way to reference and trigger the toast in a redux action.

feleko commented 3 years ago

Yes it's possible. You have to register toast.

That work for me

<ToastContext.Consumer>
    {({toast})
        => {
        registerToast(toast);
        return (
            <>
                //Your code
            </>
        )
    }}
</ToastContext.Consumer>
export let toastFn: (options: ToastConfig) => void;

export function registerToast(fn: (options: ToastConfig) => void): void {
  toastFn = fn;
}

@HridayK97 That's what you meant?

andrefangeloni commented 2 years ago

@feleko Thanks for your support, but for me this solution didn't help, because JSX is still necessary. There is a way to trigger toast on a simple function (without React Component), redux actions for example?