Skip to content

Commit dba1994

Browse files
Merge pull request LucasBassetti#191 from pascalgagneur/Removed-React-unsafe-lifecycle-methods
Removed react unsafe lifecycle methods
2 parents 5d4f5b7 + 516ddc3 commit dba1994

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
@@ -24,7 +24,9 @@ const steps = [
2424

2525
const ThemedExample = () => (
2626
<ThemeProvider theme={otherFontTheme}>
27+
<React.StrictMode>
2728
<ChatBot steps={steps} />
29+
</React.StrictMode>
2830
</ThemeProvider>
2931
);
3032

lib/ChatBot.jsx

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

58-
componentWillMount() {
58+
componentDidMount() {
5959
const { steps } = this.props;
6060
const {
6161
botDelay,
@@ -98,6 +98,23 @@ class ChatBot extends Component {
9898
chatSteps[firstStep.id].message = firstStep.message;
9999
}
100100

101+
const { recognitionEnable } = this.state;
102+
const { recognitionLang } = this.props;
103+
104+
if (recognitionEnable) {
105+
this.recognition = new Recognition(
106+
this.onRecognitionChange,
107+
this.onRecognitionEnd,
108+
this.onRecognitionStop,
109+
recognitionLang
110+
);
111+
}
112+
113+
if (this.content) {
114+
this.content.addEventListener('DOMNodeInserted', this.onNodeInserted);
115+
window.addEventListener('resize', this.onResize);
116+
}
117+
101118
const { currentStep, previousStep, previousSteps, renderedSteps } = storage.getData(
102119
{
103120
cacheName,
@@ -127,31 +144,15 @@ class ChatBot extends Component {
127144
});
128145
}
129146

130-
componentDidMount() {
131-
const { recognitionEnable } = this.state;
132-
const { recognitionLang } = this.props;
133-
134-
if (recognitionEnable) {
135-
this.recognition = new Recognition(
136-
this.onRecognitionChange,
137-
this.onRecognitionEnd,
138-
this.onRecognitionStop,
139-
recognitionLang
140-
);
141-
}
142-
143-
if (this.content) {
144-
this.content.addEventListener('DOMNodeInserted', this.onNodeInserted);
145-
window.addEventListener('resize', this.onResize);
146-
}
147-
}
148-
149-
componentWillUpdate(nextProps, nextState) {
150-
const { opened } = nextProps;
151-
152-
if (opened !== undefined && opened !== nextState.opened) {
153-
this.setState({ opened });
147+
static getDerivedStateFromProps(props, state) {
148+
const { opened, toggleFloating } = props;
149+
if (toggleFloating !== undefined && opened !== undefined && opened !== state.opened) {
150+
return {
151+
...state,
152+
opened
153+
};
154154
}
155+
return state;
155156
}
156157

157158
componentWillUnmount() {

0 commit comments

Comments
 (0)