-
Notifications
You must be signed in to change notification settings - Fork 254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use with Hooks #225
Comments
I hope it helps :) |
This seems to work, any idea how you would pass functions for onTap, onClose. |
@Lazymondaysunday pass it in context, where i am passing the ref |
I'm trying to do it, but can't figure it out. Could you paste an example? Thanks! |
@blackwolf08 I'm also not sure how to do this. E.g. how do we set the |
useDropdownAlert.js /**
* @author: Christhoval Barba <christhoval@gmail.com>
* @flow
* @format
*/
import React, {createContext, useRef, useMemo, useState, useContext} from 'react';
import {DropdownAlert} from 'components/dropdownAlert';
import type {DropdownAlertType} from 'react-native-dropdownalert';
export type AlertWithTypeFn = (
type: DropdownAlertType,
title: string,
message: string,
payload?: {[string]: string},
interval?: number,
) => void;
export type AlertContextType = {
alertWithType: AlertWithTypeFn,
};
const AlertContext: React$Context<AlertContextType> = createContext<AlertContextType>({
alertWithType() {},
});
const {Provider, Consumer} = AlertContext;
type React$Reference<T> = {current: React$ElementRef<T> | null};
export function DropdownAlertProvider({children}: {children: React$Node}): React$Element<typeof Provider> {
const [alertType, setAlertType] = useState<string>('');
const dropdown: React$Reference<DropdownAlertType> = useRef();
const contextValue: AlertContextType = useMemo(
() => ({
alertWithType: (
type: DropdownAlertType,
title: string,
message: string,
payload?: {[string]: string},
interval?: number,
) => {
setAlertType(type);
dropdown?.current?.alertWithType(type, title, message, payload, interval);
},
}),
[dropdown],
);
return (
<Provider value={contextValue}>
{React.Children.only(children)}
<DropdownAlert ref={dropdown} alertType={alertType} />
</Provider>
);
}
export function withAlert<T>(
Component: React$ComponentType<AlertContextType & T>,
): React$StatelessFunctionalComponent<T> {
return function WrapperComponent(props: T): React$Element<typeof Consumer> {
return <Consumer>{(value: AlertContextType) => <Component {...props} {...value} />}</Consumer>;
};
}
export const useDropdownAlert = (): AlertContextType => {
const context = useContext<AlertContextType>(AlertContext);
if (context === undefined) {
throw new Error('useDropdownAlert was used outside of its Provider -> DropdownAlertProvider');
}
return context;
};
export default DropdownAlertProvider; in ...
return (
<DropdownAlertProvider>
...
</DropdownAlertProvider>
);
... in Component ...
const Component = ({props}: Props) => {
const {alertWithType} = useDropdownAlert();
const showDropdown = useCallback(() => {
...
alertWithType('error', 'title', 'message');
...
}, [alertWithType]);
...
} |
@christhoval06 awesome, thanks a lot! Do you maybe have a solution for the "alert behind modal" problem as well? |
@pors current solution wrap Modal with AlertProvider and use hook. Modals use different index or elevation, then alerts and other modal is behind |
wow working great |
This solution worked but it failed when I try to execute the Test case with jest. How I Used
● InfoIcon component › InfoIcon render with onClick
|
@vishaldhanotiyadev you need provide a context provider to success test running. Example <DropdownAlertProvider>
... test
</DropdownAlertProvider > |
Short Description
Improves Documentation
Steps to Reproduce / Code Snippets / Usage
In App.js
Then in any component
The text was updated successfully, but these errors were encountered: