Skip to content

Commit c945fa6

Browse files
authored
Remove state management from component (#22)
1 parent be8dfd6 commit c945fa6

File tree

3 files changed

+19
-25
lines changed

3 files changed

+19
-25
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Features include:
1010
- fine-grained control over which elements can be edited, deleted, or added to
1111
- customisable UI, through simple, pre-defined [themes](#themes), or specific CSS overrides
1212
- self-contained — rendered with plain HTML/CSS, so no dependance on external UI libraries
13+
- provide your own [custom component](#custom-nodes) to integrate specialised UI for certain data.
1314

1415
**[Explore the Demo](https://carlosnz.github.io/json-edit-react/)**
1516

@@ -46,14 +47,21 @@ or
4647

4748
## Implementation
4849

49-
```js
50+
```jsx
5051
import { JsonEditor } from 'json-edit-react'
5152

5253
// In your React components:
53-
<JsonEditor data={ myDataObject } { ...props }>
54-
54+
<JsonEditor
55+
data={ myData }
56+
onUpdate={ ({newData} ) => {
57+
// Set "myData" state, plus
58+
// any desired side-effects.
59+
}}
60+
{ ...otherProps } />
5561
```
5662

63+
You are responsible for maintaining the data state — in your `onUpdate` function, use the `newData` property to set `data`, which should update inside the editor.
64+
5765
## Usage
5866

5967
**(for end user)**
@@ -68,7 +76,7 @@ It's pretty self explanatory (click the "edit" icon to edit, etc.), but there ar
6876

6977
## Props overview
7078

71-
The only *required* value is `data`.
79+
The only *required* value is `data`.
7280

7381
| prop | type | default | description |
7482
| ----------------------- | -------------------------------------------- | ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

src/CollectionNode.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export const CollectionNode: React.FC<CollectionNodeProps> = ({
187187

188188
return (
189189
<div
190-
className="jer-component fb-collection-component"
190+
className="jer-component jer-collection-component"
191191
style={{ marginLeft: `${path.length === 0 ? 0 : indent / 2}em` }}
192192
>
193193
{CustomNode ? (

src/JsonEditor.tsx

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
1-
import React, { useCallback, useEffect, useState } from 'react'
1+
import React, { useCallback, useEffect } from 'react'
22
import assign from 'object-property-assigner'
33
import extract from 'object-property-extractor'
44
import clone from 'just-clone'
55
import { CollectionNode, isCollection } from './CollectionNode'
6-
import {
7-
CollectionData,
8-
JsonEditorProps,
9-
FilterFunction,
10-
OnChangeFunction,
11-
TypeFilterFunction,
12-
} from './types'
6+
import { CollectionData, JsonEditorProps, FilterFunction, OnChangeFunction } from './types'
137
import { useTheme, ThemeProvider } from './theme'
148
import { getTranslateFunction } from './localisation'
159
import './style.css'
1610
import { ValueNodeWrapper } from './ValueNodeWrapper'
1711

1812
const Editor: React.FC<JsonEditorProps> = ({
19-
data: srcData,
13+
data,
2014
// schema,
2115
rootName = 'root',
2216
onUpdate,
@@ -43,15 +37,10 @@ const Editor: React.FC<JsonEditorProps> = ({
4337
className,
4438
customNodeDefinitions = [],
4539
}) => {
46-
const [data, setData] = useState<object>(srcData)
4740
const { styles, setTheme, setIcons } = useTheme()
4841
const collapseFilter = useCallback(getFilterFunction(collapse), [collapse])
4942
const translate = useCallback(getTranslateFunction(translations), [translations])
5043

51-
useEffect(() => {
52-
setData(srcData)
53-
}, [srcData])
54-
5544
useEffect(() => {
5645
if (theme) setTheme(theme)
5746
if (icons) setIcons(icons)
@@ -74,10 +63,9 @@ const Editor: React.FC<JsonEditorProps> = ({
7463
name: path.slice(-1)[0],
7564
path,
7665
})
77-
if (result === undefined) setData(newData)
7866
if (result === false) return translate('ERROR_UPDATE')
7967
return result // Error string
80-
} else setData(newData)
68+
}
8169
}
8270

8371
const onDelete: OnChangeFunction = async (value, path) => {
@@ -96,10 +84,9 @@ const Editor: React.FC<JsonEditorProps> = ({
9684
name: path.slice(-1)[0],
9785
path,
9886
})
99-
if (result === undefined) setData(newData)
10087
if (result === false) return translate('ERROR_DELETE')
10188
return result // Error string
102-
} else setData(newData)
89+
}
10390
}
10491

10592
const onAdd: OnChangeFunction = async (value, path) => {
@@ -118,10 +105,9 @@ const Editor: React.FC<JsonEditorProps> = ({
118105
name: path.slice(-1)[0],
119106
path,
120107
})
121-
if (result === undefined) setData(newData)
122108
if (result === false) return translate('ERROR_ADD')
123109
return result // Error string
124-
} else setData(newData)
110+
}
125111
}
126112

127113
const restrictEditFilter = getFilterFunction(restrictEdit)

0 commit comments

Comments
 (0)