Skip to content

Commit

Permalink
Merge pull request #5055 from matuzalemsteles/issue-5054
Browse files Browse the repository at this point in the history
fix(@clayui/provider): stabilizes the DataClient reference
  • Loading branch information
matuzalemsteles authored Aug 29, 2022
2 parents c47835a + 319bff8 commit 31234ce
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions packages/clay-provider/src/Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import {ClayIconSpriteContext} from '@clayui/icon';
import React, {useContext} from 'react';
import React, {useContext, useMemo} from 'react';

import {DataClient} from './DataClient';

Expand Down Expand Up @@ -48,15 +48,22 @@ export const Provider = ({
storageMaxSize = 20,
theme,
...otherProps
}: IProviderProps) => (
<Context.Provider
value={{client: new DataClient({storageMaxSize}), theme, ...otherProps}}
>
<ClayIconSpriteContext.Provider value={spritemap}>
{theme ? <div className={theme}>{children}</div> : children}
</ClayIconSpriteContext.Provider>
</Context.Provider>
);
}: IProviderProps) => {
// Use `useMemo` to instantiate the DataClient only once and when
// updating the property.
const client = useMemo(
() => new DataClient({storageMaxSize}),
[storageMaxSize]
);

return (
<Context.Provider value={{client, theme, ...otherProps}}>
<ClayIconSpriteContext.Provider value={spritemap}>
{theme ? <div className={theme}>{children}</div> : children}
</ClayIconSpriteContext.Provider>
</Context.Provider>
);
};

export const useProvider = () => {
return useContext(Context);
Expand Down

0 comments on commit 31234ce

Please sign in to comment.