Skip to content

Commit

Permalink
fix: route switch wrappers re-rendering (#23)
Browse files Browse the repository at this point in the history
* fix: route switch wrappers re-rendering

* feat: update version to v0.8.4
  • Loading branch information
Valerioageno authored Aug 14, 2024
1 parent 90782ae commit 9835a4c
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 37 deletions.
2 changes: 1 addition & 1 deletion crates/tuono/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tuono"
version = "0.8.3"
version = "0.8.4"
edition = "2021"
authors = ["V. Ageno <[email protected]>"]
description = "The react/rust fullstack framework"
Expand Down
4 changes: 2 additions & 2 deletions crates/tuono_lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tuono_lib"
version = "0.8.3"
version = "0.8.4"
edition = "2021"
authors = ["V. Ageno <[email protected]>"]
description = "The react/rust fullstack framework"
Expand Down Expand Up @@ -32,7 +32,7 @@ regex = "1.10.5"
either = "1.13.0"
tower-http = {version = "0.5.2", features = ["fs"]}

tuono_lib_macros = {path = "../tuono_lib_macros", version = "0.8.3"}
tuono_lib_macros = {path = "../tuono_lib_macros", version = "0.8.4"}
# Match the same version used by axum
tokio-tungstenite = "0.21.0"
futures-util = { version = "0.3", default-features = false, features = ["sink", "std"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/tuono_lib_macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tuono_lib_macros"
version = "0.8.3"
version = "0.8.4"
edition = "2021"
description = "The react/rust fullstack framework"
keywords = [ "react", "typescript", "fullstack", "web", "ssr"]
Expand Down
2 changes: 1 addition & 1 deletion packages/fs-router-vite-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tuono-fs-router-vite-plugin",
"version": "0.8.3",
"version": "0.8.4",
"description": "Plugin for the tuono's file system router. Tuono is the react/rust fullstack framework",
"scripts": {
"dev": "vite build --watch",
Expand Down
2 changes: 1 addition & 1 deletion packages/lazy-fn-vite-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tuono-lazy-fn-vite-plugin",
"version": "0.8.3",
"version": "0.8.4",
"description": "Plugin for the tuono's lazy fn. Tuono is the react/rust fullstack framework",
"scripts": {
"dev": "vite build --watch",
Expand Down
2 changes: 1 addition & 1 deletion packages/router/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tuono-router",
"version": "0.8.3",
"version": "0.8.4",
"description": "React routing component for the framework tuono. Tuono is the react/rust fullstack framework",
"scripts": {
"dev": "vite build --watch",
Expand Down
71 changes: 43 additions & 28 deletions packages/router/src/components/RouteMatch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ export const RouteMatch = ({
}: MatchProps): JSX.Element => {
const { data, isLoading } = useServerSideProps(route, serverSideProps)

const routes = React.useMemo(() => {
const components = loadParentComponents(route)
components.push(route)
return components
}, [route.id])
const routes = React.useMemo(() => loadParentComponents(route), [route.id])

return (
<TraverseRootComponents routes={routes} data={data} isLoading={isLoading} />
<TraverseRootComponents routes={routes} data={data} isLoading={isLoading}>
<route.component data={data} isLoading={isLoading} />
</TraverseRootComponents>
)
}

Expand All @@ -44,29 +42,46 @@ interface ParentProps {
isLoading: boolean
}

const TraverseRootComponents = ({
routes,
data,
isLoading,
index = 0,
}: TraverseRootComponentsProps): JSX.Element => {
const Parent = React.memo(
routes[index]?.component as unknown as (props: ParentProps) => JSX.Element,
)
/*
* This component traverses and renders
* all the components that wraps the selected route (__root).
* The parents components need to be memoized in order to avoid
* re-rendering bugs when changing route.
*/
const TraverseRootComponents = React.memo(
({
routes,
data,
isLoading,
index = 0,
children,
}: TraverseRootComponentsProps): JSX.Element => {
if (routes.length > index) {
const Parent = React.useMemo(
() =>
routes[index]?.component as unknown as (
props: ParentProps,
) => JSX.Element,
[],
)

return (
<Parent data={data} isLoading={isLoading}>
{Boolean(routes.length > index) && (
<TraverseRootComponents
routes={routes}
data={data}
isLoading={isLoading}
index={index + 1}
/>
)}
</Parent>
)
}
return (
<Parent data={data} isLoading={isLoading}>
<TraverseRootComponents
routes={routes}
data={data}
isLoading={isLoading}
index={index + 1}
>
{children}
</TraverseRootComponents>
</Parent>
)
}

return <>{children}</>
},
)

const loadParentComponents = (route: Route, loader: Route[] = []): Route[] => {
const parentComponent = route.options?.getParentRoute?.()
Expand Down
3 changes: 2 additions & 1 deletion packages/router/src/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { RouterType } from './router'
import type { RouteComponent } from './types'
import { trimPathLeft, joinPaths } from './utils'

interface RouteOptions {
Expand All @@ -25,7 +26,7 @@ export class Route {
router: RouterType
isRoot: boolean
originalIndex?: number
component: () => JSX.Element
component: RouteComponent

constructor(options: RouteOptions) {
this.isRoot = options.isRoot ?? typeof options.getParentRoute !== 'function'
Expand Down
7 changes: 7 additions & 0 deletions packages/router/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ export interface ServerProps {
}
props: any
}

export interface RouteProps {
data: any
isLoading: boolean
}

export type RouteComponent = (props: RouteProps) => JSX.Element
2 changes: 1 addition & 1 deletion packages/tuono/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tuono",
"version": "0.8.3",
"version": "0.8.4",
"description": "The react/rust fullstack framework",
"scripts": {
"dev": "vite build --watch",
Expand Down

0 comments on commit 9835a4c

Please sign in to comment.