Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,24 @@ const searchAPI = text => fetch('/search?text=' + encodeURIComponent(text));
const searchAPIDebounced = AwesomeDebouncePromise(searchAPI, 500);

class SearchInputAndResults extends React.Component {
mounted = true;

state = {
text: '',
results: null,
};

handleTextChange = async text => {
this.setState({ text, results: null });
const result = await searchAPIDebounced(text);
this.setState({ result });
handleTextChange = text => {
this.setState({ text, results: null }, async () => {
const results = await searchAPIDebounced(text);
if (this.mounted) {
this.setState({ results });
}
});
};

componentWillUnmount() {
this.setState = () => {};
this.mounted = false;
}
}
```
Expand Down Expand Up @@ -114,9 +119,10 @@ class SearchInputAndResults extends React.Component {
value2: '',
};

onFieldTextChange = async (fieldId, fieldValue) => {
this.setState({ [fieldId]: fieldValue });
await saveFieldValueDebounced(fieldId, fieldValue);
onFieldTextChange = (fieldId, fieldValue) => {
this.setState({ [fieldId]: fieldValue }, async () => {
await saveFieldValueDebounced(fieldId, fieldValue);
});
};

render() {
Expand Down Expand Up @@ -172,12 +178,13 @@ The debouncing function returned by the lib is stateful. If you want deboucing t
Instead of this:

```js
handleTextChange = async text => {
handleTextChange = text => {
const searchAPI = text => fetch('/search?text=' + encodeURIComponent(text));
const searchAPIDebounced = AwesomeDebouncePromise(searchAPI, 500);
this.setState({ text, results: null });
const result = await searchAPIDebounced(text);
this.setState({ result });
this.setState({ text, results: null }, async () => {
const results = await searchAPIDebounced(text);
this.setState({ results });
});
};
```

Expand All @@ -187,10 +194,11 @@ Do this:
const searchAPI = text => fetch('/search?text=' + encodeURIComponent(text));
const searchAPIDebounced = AwesomeDebouncePromise(searchAPI, 500);

handleTextChange = async text => {
this.setState({ text, results: null });
const result = await searchAPIDebounced(text);
this.setState({ result });
handleTextChange = text => {
this.setState({ text, results: null }, async () => {
const results = await searchAPIDebounced(text);
this.setState({ results });
});
};
```

Expand Down