-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix fast-refresh and URL updates when using preview
- Loading branch information
1 parent
62a9aba
commit f9dbad6
Showing
5 changed files
with
87 additions
and
72 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,31 @@ | ||
const { AppRegistry, View } = require("react-native"); | ||
|
||
global.__RNIDE_previews ||= new Map(); | ||
export const PREVIEW_APP_KEY = "RNIDE_preview"; | ||
|
||
function stringifyProps(obj) { | ||
const keyValuePairs = []; | ||
global.__RNIDE_previews ||= new Map(); | ||
|
||
for (let key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
keyValuePairs.push(`${key}=${obj[key]}`); | ||
} | ||
export function Preview({ previewKey }) { | ||
const previewData = global.__RNIDE_previews.get(previewKey); | ||
if (!previewData || !previewData.component) { | ||
return null; | ||
} | ||
|
||
return keyValuePairs.join(" "); | ||
return ( | ||
<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}> | ||
{previewData.component} | ||
</View> | ||
); | ||
} | ||
|
||
export function preview(component) { | ||
if (component === undefined || component._source === null) { | ||
if (!component || component._source === null) { | ||
return; | ||
} | ||
|
||
const name = `preview:/${component._source.fileName}:${component._source.lineNumber}`; | ||
function Preview() { | ||
return ( | ||
<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>{component}</View> | ||
); | ||
} | ||
global.__RNIDE_previews.set(name, { | ||
appKey: name, | ||
const key = `preview:/${component._source.fileName}:${component._source.lineNumber}`; | ||
global.__RNIDE_previews.set(key, { | ||
component, | ||
name: component.type.name, | ||
props: stringifyProps(component.props), | ||
fileName: component._source.fileName, | ||
lineNumber: component._source.lineNumber, | ||
}); | ||
AppRegistry.registerComponent(name, () => Preview); | ||
} | ||
|
||
AppRegistry.registerComponent(PREVIEW_APP_KEY, () => Preview); |
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