You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`react-from-json` also supports flat, non-recursive structures via the special `<ComponentLookup />` component. This is useful when working with typed systems like GraphQL, and you need to avoid unions.
81
+
82
+
The `<ComponentLookup />` simply maps to another component defined in a `components` object. If you were using it in React, you would use it like:
which would look up the `Button` component at index `0` in the `components` object, resolving to:
89
+
90
+
```jsx
91
+
<Button>Hello, World!</Button>
92
+
```
93
+
94
+
For `react-from-json` we use JSON, so we would write this:
95
+
96
+
```json
97
+
{
98
+
"type": "ComponentLookup",
99
+
"props": {
100
+
"componentType": "Button",
101
+
"componentIndex": 0
102
+
}
103
+
}
104
+
```
81
105
82
-
`react-from-json` also supports non-recursive structures via the special `ComponentRef` prop. This is useful when working with typed systems like GraphQL, and you need to avoid unions.
106
+
#### Example
83
107
84
-
Here's the same example as above, instead using a `ComponentRef` for `entry.props.patty`, and providing a separate `components` object.
108
+
Here's the same example as above, instead using a `<ComponentLookup />` for `entry.props.patty`, and providing a separate `components` object.
85
109
86
110
```jsx
87
111
importReactfrom"react";
@@ -92,8 +116,11 @@ const entry = {
92
116
props: {
93
117
chain:"Wahlburger",
94
118
patty: {
95
-
componentIndex:0,
96
-
componentType:"Patty"
119
+
type:"ComponentLookup",
120
+
props: {
121
+
componentIndex:0,
122
+
componentType:"Patty"
123
+
}
97
124
}
98
125
}
99
126
};
@@ -126,15 +153,6 @@ const Example = () => {
126
153
};
127
154
```
128
155
129
-
The `ComponentRef` prop looks like:
130
-
131
-
```ts
132
-
interfaceComponentRef {
133
-
componentIndex:number;
134
-
componentType:string;
135
-
}
136
-
```
137
-
138
156
## With TypeScript
139
157
140
158
`react-from-json` supports generic types for use with TypeScript.
throw"Detected `ComponentLookup` prop on a component, but `components` is undefined. You need to define `components` if using `ComponentLookup` props.";
0 commit comments