-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathindex.jsx
More file actions
66 lines (62 loc) · 2.34 KB
/
index.jsx
File metadata and controls
66 lines (62 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Copyright (c) 2021, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import React, {useContext, useState} from 'react'
import PropTypes from 'prop-types'
import {Switch as RouterSwitch, Route} from 'react-router-dom'
import AppErrorBoundary from '../app-error-boundary'
import {UIDReset, UIDFork} from 'react-uid'
// TODO: can the context be moved to a HOC so routes don't always have to be serialized
const RoutesContext = React.createContext({})
export const useRoutesContext = () => useContext(RoutesContext)
/**
* The Switch component packages up the bits of rendering that are shared between
* server and client-side. It's *mostly* a react-router Switch component, hence the
* name.
*
* This is for internal use only.
*
* @private
*/
const Switch = (props) => {
const {error, appState, routes, App} = props
const [_routes, setRoutes] = useState(routes)
return (
<UIDReset>
<AppErrorBoundary error={error}>
{!error && (
<RoutesContext.Provider value={{
routes: _routes,
setRoutes
}}>
<App preloadedProps={appState.appProps}>
<RouterSwitch>
{_routes.map((route, i) => {
const {component: Component, ...routeProps} = route
return (
<Route key={i} {...routeProps}>
<UIDFork>
<Component preloadedProps={appState.pageProps} />
</UIDFork>
</Route>
)
})}
</RouterSwitch>
</App>
</RoutesContext.Provider>
)}
</AppErrorBoundary>
</UIDReset>
)
}
Switch.propTypes = {
error: PropTypes.object,
appState: PropTypes.object,
routes: PropTypes.array,
App: PropTypes.func,
preloadedProps: PropTypes.object
}
export default Switch