on 2de2112 help sign in
bridge/src/store/pointCache.js
38 lines 906 B history raw
1import React, { createContext, forwardRef, useContext, useState } from 'react';
3export const PointCacheContext = createContext(null);
5function _usePointCache() {
6 const [pointCache, _setPointCache] = useState({});
8 const addToPointCache = entry =>
9 _setPointCache(cache => ({ ...cache, ...entry }));
11 return {
12 pointCache,
13 addToPointCache,
14 };
17export function PointCacheProvider({ children }) {
18 const pointCache = _usePointCache();
20 return (
21 <PointCacheContext.Provider value={pointCache}>
22 {children}
23 </PointCacheContext.Provider>
24 );
27// Hook version
28export function usePointCache() {
29 return useContext(PointCacheContext);
32// HOC version
33export const withPointCache = Component =>
34 forwardRef((props, ref) => (
35 <PointCacheContext.Consumer>
36 {pointCache => <Component ref={ref} {...pointCache} {...props} />}
37 </PointCacheContext.Consumer>
38 ));