@@ -21,6 +21,7 @@ export type ContextProps = {
2121 navigation : RouterNavigator ;
2222 page : X . Nullable < ConfiguredRoute > ;
2323 paths : { } ;
24+ setLoading : ( b : boolean ) => void ;
2425} ;
2526
2627const Context = createContext < ContextProps | undefined > ( undefined ) ;
@@ -59,18 +60,18 @@ const findMatches = (config: Base, pathName: string, filter: BroutherProps<any>[
5960 */
6061export const Brouther = < T extends Base > ( { config, ErrorElement, children, filter } : BroutherProps < T > ) => {
6162 const [ state , setState ] = useState ( ( ) => ( {
62- loading : false ,
6363 error : null as X . Nullable < BroutherError > ,
6464 location : config . history . location ,
6565 loaderData : null as X . Nullable < Response > ,
6666 matches : findMatches ( config , config . history . location . pathname , filter ) ,
6767 } ) ) ;
68+ const [ loading , setLoading ] = useState ( false ) ;
6869
6970 useEffect ( ( ) => {
7071 const result = findMatches ( config , state . location . pathname , filter ) ;
7172 const request = async ( ) => {
7273 if ( result ?. page ?. loader ) {
73- setState ( ( p ) => ( { ... p , loading : true } ) ) ;
74+ setLoading ( true ) ;
7475 const search = new URLSearchParams ( state . location . search ) ;
7576 const qs = transformData ( search , mapUrlToQueryStringRecord ( result . page . originalPath , fromStringToValue ) ) ;
7677 const s = ( state . location . state as any ) ?? { } ;
@@ -81,11 +82,12 @@ export const Brouther = <T extends Base>({ config, ErrorElement, children, filte
8182 data : result . page . data ?? { } ,
8283 request : new Request ( s . url ?? href , { body : s . body ?? undefined , headers : s . headers } ) ,
8384 } ) ;
85+ setLoading ( false ) ;
8486 return { loaderData : r , loading : false } ;
8587 }
8688 } ;
8789 setState ( ( p ) => ( { ...p , matches : result , error : result . error ?? null } ) ) ;
88- request ( ) . then ( ( x ) => setState ( ( prev ) => ( { ...prev , ...x } ) ) ) ;
90+ request ( ) . then ( ( result ) => setState ( ( prev ) => ( { ...prev , ...result } ) ) ) ;
8991 } , [ findMatches , state . location . search , state . location . state , config , filter , state . location . pathname ] ) ;
9092
9193 useEffect ( ( ) => {
@@ -105,11 +107,12 @@ export const Brouther = <T extends Base>({ config, ErrorElement, children, filte
105107 error : state . matches . error ?? state . error ,
106108 href,
107109 loaderData : state . loaderData ,
108- loading : state . loading ,
110+ loading,
109111 location : state . location ,
110112 navigation : config . navigation ,
111- paths : state . matches . params ,
112113 page : state . error !== null ? null : state . matches . page ,
114+ paths : state . matches . params ,
115+ setLoading,
113116 } }
114117 >
115118 < CatchError fallback = { Fallback } state = { state } setError = { setError } >
@@ -214,7 +217,29 @@ export function useDataLoader<T extends DataLoader>(fn: (response: Response) =>
214217 return state ;
215218}
216219
220+ /*
221+ Get current error and the current page that throw the error
222+ @returns string
223+ */
217224export const useRouteError = ( ) => {
218225 const router = useRouter ( ) ;
219226 return [ router . error , router . page ] as const ;
220227} ;
228+
229+ /*
230+ Render the page that match with your route
231+ @returns string
232+ */
233+ export const Outlet = ( ) => {
234+ const page = usePage ( ) ;
235+ return < Fragment > { page } </ Fragment > ;
236+ } ;
237+
238+ /*
239+ Boolean that represents if it's your action/loader process is loading
240+ @returns string
241+ */
242+ export const useLoadingState = ( ) => {
243+ const router = useRouter ( ) ;
244+ return router . loading ;
245+ } ;
0 commit comments