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
Copy file name to clipboardExpand all lines: README.md
+42Lines changed: 42 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -75,6 +75,48 @@ const Example = () => {
75
75
};
76
76
```
77
77
78
+
### Other JSON shapes
79
+
80
+
If your data doesn't follow the `type` | `props` shape, `react-from-json` makes it easy to map your data on the fly using the `mapProp` prop.
81
+
82
+
```jsx
83
+
importReactfrom"react";
84
+
importReactFromJSONfrom"react-from-json";
85
+
importmappingfrom"./mapping";
86
+
87
+
constentryWithDifferentShape= {
88
+
_type:"Burger",
89
+
chain:"Wahlburger",
90
+
children: {
91
+
_type:"Patty",
92
+
variant:"Impossible"
93
+
}
94
+
};
95
+
96
+
constmapProp=prop=> {
97
+
if (prop._type) {
98
+
const { _type, ...props } = prop;
99
+
100
+
return {
101
+
type: _type,
102
+
props
103
+
};
104
+
}
105
+
106
+
return prop;
107
+
};
108
+
109
+
constExample= () => {
110
+
return (
111
+
<ReactFromJSON
112
+
entry={entryWithDifferentShape}
113
+
mapping={mapping}
114
+
mapProp={mapProp}
115
+
/>
116
+
);
117
+
};
118
+
```
119
+
78
120
### Flat trees
79
121
80
122
`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.
0 commit comments