arnnis / react-native-toast-notifications Goto Github PK
View Code? Open in Web Editor NEWToast component for React Native, supports Android, iOS and Web
Toast component for React Native, supports Android, iOS and Web
Hey, I like the library, thanks for open sourcing. Have you considered using createContext
with a provider to put the ref at the root of the app, and then a useToast
hook? I think it would be better than using global.toast
, since it stays more in the react world.
Is there a way to change the horizontal align from "center" to "right" or "left" ?
When using {placement:'top'}
the animation of the toast changes to the top animation however the toast remains on the bottom of the page.
Toast should be on the top of the page when placement is set to top
software | version |
---|---|
ios or android | |
react-native | 0.63.4 |
react-native-fast-toast | 2.2.0 |
node | |
npm or yarn |
hi, love your works, do we have center placement for toast?
Hey.
Do you have any plans for supporting swipe to close?
Great job, smooth and easy to implement.
I couldn't find a way to close a toast when clicked. Any suggestion ?
toastWarnings doesnt work inside Modal
expected to work inside modal, not in parent component
running app via USB
Is there way to disable animation?
Toast container is not 100% width, this is a new issue caused by this commit.
Because width in container style was deleted and it value is replaced with width from Dimensions. But only center toast use width prop, top and bottom lack this prop. I think width is required by all of those three placements of toast container.
For Syntax Highlighting check this link
Fix this issue is easy, just and width in two methods renderBottomToasts and renderTopToasts in toast-container.tsx
software | version |
---|---|
ios or android | |
react-native | |
react-native-toast-notifications | |
node | |
npm or yarn |
Hi,
I wanted to change the color for the "normal" type and added style={{ backgroundColor: 'gray' }} to it.
But now if using other types ("danger" for example) the background is still gray - at least when using it on the react native web platform.
Could you please add a "normalColor" prop?
Or is there any other way around this?
Thanks! And great lib, btw!
I saw multiple people were searching for a way to close the toast on press, and I found a simple way to make it work closeOnPress, so I just wanted to share if it could help others.
useToast.ts
import ToastContainer, {
useToast as useToastModule,
} from "react-native-fast-toast";
import { ToastOptions } from "react-native-fast-toast/lib/typescript/toast";
/**
* Toast hook extending the useToast with specific toast variants
*/
export const useToast = (
globalOptions: { duration: number; closeOnPress?: boolean } = {
duration: 5000,
closeOnPress: true,
}
) => {
const toast = useToastModule();
const success = (
message: string | JSX.Element,
options?: ToastOptions & {
onPress?: (id: string) => void;
closeOnPress?: boolean;
}
) => {
toast?.show(message, {
type: "success",
duration: globalOptions?.duration,
onPress: () => {
if (options.closeOnPress || globalOptions?.closeOnPress) toast.hide(id);
options?.onPress?.(id);
},
...options,
});
};
return { success };
};
Hi, first of all thanks for this amazing library!
I had troubles trying to make onPress work with android. Instead of calling onPress it tries to swipe left or right.
I am using the example for the FAQ https://github.com/arnnis/react-native-toast-notifications#--how-to-call-toast-outside-react-components
And a provider with customization like for example successColor: 'orange'
Calling toast.show('test',{type:'success'})
works but does not take the customizations into account and I couldn't find another way to add these customizations.
Toast Container's width set to screen width makes it not possible for element to the side to be pressed
Components next to the toast, but not underneath should be pressable
Here is a repro https://github.com/itsramiel/toast-bug-repro
This bug was introduced by #140 #135
remove setting the width propery. I will submit a pr to fix this
Hello, dear component author,
We are testing our app with appium, we can set a accessiblityLabel for any element and use it in our test script. However, I found that even if I set it for this custom toast component, it's not able to be located.
Any idea? I'd just like to verify the toast message in my test script.
Test script:
it('should display error message given wrong credentials', async () => {
await $('~username').setValue('hello');
await $('~password').setValue('world');
await $('~btnLogin').click();
// https://stackoverflow.com/questions/30458931/how-to-test-android-toast-messages-in-appium-selenium-java
// const message = await $('//android.widget.Toast[1]').getText();
const message = await $('~toastMessage').getText();
expect(message).toBe('Wrong ID or Password');
});
Component code:
<ToastProvider
duration={600000}
renderType={{
custom_type: toast => (
<View
accessibilityLabel="toastView"
style={{
backgroundColor: 'rgba(255,27,31,0.60)',
...
top: 334,
}}>
<Text
accessibilityLabel="toastMessage"
style={{ fontSize: 20, color: '#ffffff' }}>{`${toast.message}`}</Text>
</View>
),
on android new item add to last item position in array but ios added to first position
Be smooth line on desktop, ios
I'm use default ToastProvider without any props and default toast.show() without any options
attached video
upgrade to latest version 3.3.0
software | version |
---|---|
ios or android | mobile web (safari) |
react-native | 0.68.2 |
react-native-toast-notifications | 3.3.0 |
node | 16.14.1 |
npm or yarn | 1.22.18 |
After installing, when I run react-native run-android
I get the following error:
error: bundling failed: SyntaxError: /Users/nickwoods/Documents/GitHub/client-app/node_modules/react-native-toast-notifications/src/index.ts: Unexpected token (4:12)
2 | export { default as ToastProvider } from "./hook/provider";
3 | export { default as useToast } from "./hook/useToast";
> 4 | export type {ToastType} from './hook/context'
| ^
Great job with this library, I really like it. I am struggling to make the toasts build as you have in the gif example so that you can have multiple toasts showing at one time. i.e. the onPress button is clicked multiple times and each click creates a new toast, for each new toast it is rendered above the old one (or the old one is pushed up to make space for the new one).
When I try to render more than one toast, I get a red error:
"Warning: Cannot update during an existing state transition (such as within render
). Render methods should be a pure function of props and state."
How did you do it on the gif in the readme? e.g. you click on "normal" toast several times and several toasts are shown at once.
The placement
property doesn't seem to be working properly in the context of toast.show()
. The toast message just shows up on the bottom as usual.
The ToastOptions
interface doesn't include it, and while both ToastProps
do, it seems that placement
is not intended to be a property of the show
method, i.e.:
toast.show('text', {placement: 'top'})
I've tried to add a Toast as both global and in one screen and get the following error:
Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
My project is running in Expo on iOS simulator. Code is copied directly from readme.
If I want to use the toast inside react components and also in a redux store should I wrap my app in ToastProvider or can I just use:
import Toast from "react-native-toast-notifications";
export default function App() {
return (
<>
<RestOfYourApp />
<Toast ref={(ref) => global['toast'] = ref} />
</>
);
If I can use the above one in anywhere why should I wrap my app in ToastProvider? Are there any benefits
Hiya,
I was wondering what the best method is for only allowing a single notification on the screen.
I have called toast.hideAll()
before calling toast.show(...)
but it results in some animation bugs.
Any idea?
cheers
edit:
This seems to work as a patch.
const run = async () => {
toast.hideAll()
await new Promise<void>((resolve) => setTimeout(() => resolve(), 500))
toast.show(text, { type: variant })
}
void run()
Nice work,
I use React-Native with TypeScript and I define the toast globally on App.tsx
like this:
<Provider store={store}>
<NavigationContainer>
<Stack.Navigator>
//....
</Stack.Navigator>
</NavigationContainer>
<Toast ref={(ref) => global["toast"] = ref} placement={"top"} />
</Provider>
And I get the Warning:
TS7053: Element implicitly has an 'any' type because expression of type '"toast"' can't be used to index type 'Global & typeof globalThis'. Property 'toast' does not exist on type 'Global & typeof globalThis'.
How can I fix this?
Thanks in advance.
Toast is modal
Unable to click anything else underneath
a parameter that can override this toast modality
If the StatusBar has a background color, the Toast with placement "Top" is partially hidden by the StatusBar
Toast calculate the margin with reference to the StatusBar size
toast.show(message, {
type: 'danger',
placement: 'top',
duration: 4000,
animationType: 'slide-in',
});
toast.show(message, {
type: 'danger',
placement: 'top',
duration: 4000,
style: { marginTop: StatusBar.currentHeight }, // <- This solve the problem!
animationType: 'slide-in',
});
software | version |
---|---|
ios or android | 9 |
react-native | 0.64.3 |
react-native-toast-notifications | 3.2.3 |
node | 16.13.0 |
npm or yarn | Yarn 1.23.0-20210726.1745 |
I can't access offset prop in toast.show method.
We should be able to access offset (ideally also bottomOffset and topOffset, but that is not mentioned in documentation, so let's focus just on offset).
Documentation example states this:
toast.show("Task finished successfully", {
type: "normal | success | warning | danger | custom",
placement: "top | bottom",
duration: 4000,
offset: 30 <========== I can't access this.
animationType: "slide-in | zoom-in",
});
Snack example:
https://snack.expo.dev/iidWBs8MB
//
When i set offset (also top and bottom) in provider, it works. But that is global setting i would like to override.
software | version |
---|---|
ios or android | android |
react-native | 0.64.2 |
react-native-toast-notifications | 3.2.2 |
node | |
npm or yarn | yarn |
let id = toast.show("Loading...");
toast.update(id, "Loading completed", {type: "success"});
toast.update function is not working.
Is there any way to make toast messages in to queue (latest messages in front and old messages in back) instead of showing like a stack?
Hello !
Can I make possible the swipe up with your config options please ?
Thanks
I have used the toast to display when the authToken gets expire. But when multiple apis are called, it shows multiple times. Is there any way to make sure that only one toast appears at a time?
I am using react native modal. But in modal screen I am not able to show toast. Any solution.
Could it be possible to set an option to keep a toast open until closed manually?
It would be useful for toasts that are very important.
If I have a modal the toast will be blocked by Modal
in react-native-web.
I find out the z-index of toast was set to 999, and the default z-index for Modal was 9999.
If there is a props allows customizing the z-index level that will be great.
For Syntax Highlighting check this link
Was thinking to overwrite the z-index with css, but since css class name is randomize so i couldn't target it.
software | version |
---|---|
chrome | 92.0.4515.131 |
react | 17 |
react-native-toast-notifications | 3.1.0 |
node | 14.16.1 |
npm | 6.14.12 |
The ToastOptions
type is not exported and so consumers can not use it to compose custom types for their use cases.
Ex:
import { ToastOptions } from 'react-native-toast-notifications';
type MyToastOptions = ToastOptions & {
msg: string;
icon: React.ReactNode;
};
The ToastOptions
type is exported
I created a patch for now
diff --git a/node_modules/react-native-toast-notifications/lib/typescript/index.d.ts b/node_modules/react-native-toast-notifications/lib/typescript/index.d.ts
index 01bde8b..9005af6 100644
--- a/node_modules/react-native-toast-notifications/lib/typescript/index.d.ts
+++ b/node_modules/react-native-toast-notifications/lib/typescript/index.d.ts
@@ -2,3 +2,4 @@ export { default } from "./toast-container";
export { default as ToastProvider } from "./hook/provider";
export { default as useToast } from "./hook/useToast";
export type { ToastType } from './hook/context';
+export type { ToastOptions } from './toast';
\ No newline at end of file
software | version |
---|---|
ios or android | |
react-native | |
react-native-toast-notifications | 3.2.3 |
node | |
npm or yarn |
When using the placement="top"
param on the ToastProvider
, toast messages still default to bottom
placement.
When using the placement="top"
param on the ToastProvider
, toast messages should default to top
placement.
For Syntax Highlighting check this link
const App = () =>
<ToastProvider placement="top">
<MyComponent />
</ToastProvider>
const MyComponent = () => {
const toast = useToast();
useEffect(() => {
toast.show("Hello from top");
}, []);
return <Text>Hello World</Text>;
}
software | version |
---|---|
ios | iOS 14 |
react-native | 0.64.2 |
react-native-fast-toast | ^3.0.0 |
node | |
npm |
Guys thank you so much for the great library! Please can you help me with this error))
The app crashes with error:
toast.show is not a function. (In 'toast.show('Hello World')', 'toast.show' is undefined
After the app launching get render error.
Expect app is not crashing and notification is appearing.
I use the same code from your example:
import { useToast } from "react-native-toast-notifications";
const Component = () => {
const toast = useToast();
useEffect(() => {
toast.show("Hello World");
}, []);
};
and before it I wrapped my app.tsx to provider:
<ToastProvider>
<RestOfYourApp />
<ToastProvider/>
software | version |
---|---|
ios or android | both platform |
react-native | 0.64 |
react-native-toast-notifications | 3.2.3 |
node | 14.18.3 |
yarn | 1.22.17 |
For Syntax Highlighting check this link
software | version |
---|---|
ios or android | |
react-native | |
react-native-toast-notifications | |
node | |
npm or yarn |
I am using a button to show and then hide a toast with the same ID.
Somehow I can't manually get the toast to hide. I am making sure to use a unique ID, used only for this one toast.
I can update some things like "message" or "type", which are reflected immediately, but even if I update duration to be only 1 second, it still wont go away, until I interact with the screen.
toast.hideAll() also doesnt work until the screen has been touched,
I am providing Toast using the alternative way, as shown here:
https://github.com/arnnis/react-native-toast-notifications#--how-to-call-toast-outside-react-components
This is all happening with a button press inside a component.
Any help please?
Current behaviour:-
Hi team in Android 12 the OnPress is not working, on Android 10 it is working fine
Expected behaviour:-
the onPress should work in android 12
const chattoast = useToast();
chattoast.show(toastMessage, {
type: "success",
onClose: () => {
console.log("CLOSE HANDLER")
},
onPress: () => {
chattoast.hideAll()
console.log("Navigation HANDLER")
chatNavigationHandler()
},
style: {
marginBottom: 100
},
duration: 5000,
})
Environment
software | version |
---|---|
android | 12 |
react-native | 0.66.3 |
react-native-toast-notifications | 3.2.3 |
node | v14.17.5 |
npm or yarn | yarn |
I'm showing an error toast while the keyboard is open, and it renders below the keyboard.
This is on iOS, and I haven't tested Android (yet, I will).
The toast's bottom offset should adjust to the keyboard height so it is visible when shown.
If necessary I will make one, I am just busy for the rest of today.
I have not tried any manual workarounds yet, but my initial thought would be to use react-native-keyboard-height, and pass the keyboard height to the bottom offset at the callsite. However, if the keyboard was to close while the toast is showing, it would not move, so it's not an ideal solution.
software | version |
---|---|
ios or android | ios12 |
react-native | 0.63.3 |
react-native-fast-toast | 1.1.0 |
node | 10 |
npm or yarn | 1.22.5 |
If a code sample is necessary I will provide one as soon as I get the chance, I'm just busy for the rest of today. I will edit this as and when I do that.
I could also probably make a PR to fix this myself.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.