Skip to content

Commit

Permalink
react:feat Props loader indicators
Browse files Browse the repository at this point in the history
  • Loading branch information
frandiox committed Apr 6, 2021
1 parent d74560a commit 62df6d6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion examples/react/src/pages/post/index.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import React from 'react'

const Post = ({ params = {} }) => {
const Post = ({ params = {}, isLoadingProps, isRevalidatingProps }) => {
if (!import.meta.env.SSR) {
console.log({ isLoadingProps, isRevalidatingProps })
}

return (
<div>
<h1>Post #{params.postId}</h1>
Expand Down
12 changes: 12 additions & 0 deletions src/react/entry-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export function PropsProvider({
lastRoutePath = to.path

const [state, setState] = useState(to.meta.state)
let isLoadingProps = false
let isRevalidatingProps = false

if (!to.meta.state || isChangingRoute) {
if (from && to.path === from.path) {
Expand All @@ -35,6 +37,12 @@ export function PropsProvider({
const propsRoute = buildPropsRoute(to)

if (propsRoute) {
if (state) {
isRevalidatingProps = true
} else {
isLoadingProps = true
}

fetch(propsRoute.fullPath, {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
Expand All @@ -46,13 +54,17 @@ export function PropsProvider({
})
.catch((error) => {
console.error(error)
to.meta.state = { error }
setState(to.meta.state)
})
}
}
}

const { passToPage } = pagePropsOptions || {}
return React.createElement(Page, {
isLoadingProps,
isRevalidatingProps,
...((passToPage && state) || {}),
...rest,
})
Expand Down

0 comments on commit 62df6d6

Please sign in to comment.