-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out createElements from resolveElements
- Loading branch information
Showing
3 changed files
with
48 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React from 'react'; | ||
import warning from 'warning'; | ||
|
||
import { isResolved } from './ResolverUtils'; | ||
|
||
export default function createElements(routeMatches, Components, matchData) { | ||
return routeMatches.map((match, i) => { | ||
const { route } = match; | ||
|
||
const Component = Components[i]; | ||
const data = matchData[i]; | ||
|
||
const isComponentResolved = isResolved(Component); | ||
const areDataResolved = isResolved(data); | ||
|
||
if (route.render) { | ||
// Perhaps undefined here would be more correct for "not ready", but | ||
// Relay uses null in RelayReadyStateRenderer, so let's follow that | ||
// convention. | ||
return route.render({ | ||
match, | ||
Component: isComponentResolved ? Component : null, | ||
props: areDataResolved ? { ...match, data } : null, | ||
data: areDataResolved ? data : null, | ||
}); | ||
} | ||
|
||
if (!isComponentResolved || !areDataResolved) { | ||
// Can't render. | ||
return undefined; | ||
} | ||
|
||
if (!Component) { | ||
// Note this check would be wrong on potentially unresolved data. | ||
warning( | ||
data === undefined, | ||
`Route ${i} has data, but no render method or component.`, | ||
); | ||
|
||
// Nothing to render. | ||
return null; | ||
} | ||
|
||
return <Component {...match} data={data} />; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters