Skip to content

Commit 39f0f7d

Browse files
committed
Removed unsafe lifecycle methods function body moved to other methods
- componentWillMount moved to componentDidMount - componentWillUpdate moved to getDerivedStateFromProps - Checks that toggleFloating is defined when relying on prop opened - Wrapped example ChatBot in React.StrictMode
1 parent 8d0ac48 commit 39f0f7d

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

example/components/Example.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ const steps = [
2525

2626
const ThemedExample = () => (
2727
<ThemeProvider theme={otherFontTheme}>
28+
<React.StrictMode>
2829
<ChatBot steps={steps} />
30+
</React.StrictMode>
2931
</ThemeProvider>
3032
);
3133

lib/ChatBot.jsx

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class ChatBot extends Component {
6161
this.speak = speakFn(props.speechSynthesis);
6262
}
6363

64-
componentWillMount() {
64+
componentDidMount() {
6565
const { steps } = this.props;
6666
const {
6767
botDelay,
@@ -104,6 +104,23 @@ class ChatBot extends Component {
104104
chatSteps[firstStep.id].message = firstStep.message;
105105
}
106106

107+
const { recognitionEnable } = this.state;
108+
const { recognitionLang } = this.props;
109+
110+
if (recognitionEnable) {
111+
this.recognition = new Recognition(
112+
this.onRecognitionChange,
113+
this.onRecognitionEnd,
114+
this.onRecognitionStop,
115+
recognitionLang,
116+
);
117+
}
118+
119+
if (this.content) {
120+
this.content.addEventListener('DOMNodeInserted', this.onNodeInserted);
121+
window.addEventListener('resize', this.onResize);
122+
}
123+
107124
const {
108125
currentStep,
109126
previousStep,
@@ -138,31 +155,15 @@ class ChatBot extends Component {
138155
});
139156
}
140157

141-
componentDidMount() {
142-
const { recognitionEnable } = this.state;
143-
const { recognitionLang } = this.props;
144-
145-
if (recognitionEnable) {
146-
this.recognition = new Recognition(
147-
this.onRecognitionChange,
148-
this.onRecognitionEnd,
149-
this.onRecognitionStop,
150-
recognitionLang,
151-
);
152-
}
153-
154-
if (this.content) {
155-
this.content.addEventListener('DOMNodeInserted', this.onNodeInserted);
156-
window.addEventListener('resize', this.onResize);
157-
}
158-
}
159-
160-
componentWillUpdate(nextProps, nextState) {
161-
const { opened } = nextProps;
162-
163-
if (opened !== undefined && opened !== nextState.opened) {
164-
this.setState({ opened });
158+
static getDerivedStateFromProps(props, state) {
159+
const { opened, toggleFloating } = props;
160+
if (toggleFloating !== undefined && opened !== undefined && opened !== state.opened) {
161+
return {
162+
...state,
163+
opened
164+
}
165165
}
166+
return state;
166167
}
167168

168169
componentWillUnmount() {

0 commit comments

Comments
 (0)