Remove UNSAFE lifecycle methods (#377)

* Remove UNSAFE lifecycle methods

* Fix gDSFP issue

* Extract onBeforeChange

* Remove this from getDerivedStateFromProps
main
Michael Fix 7 years ago committed by Brian Dennis
parent 11c911e6b9
commit a81107d723

@ -10,6 +10,20 @@ import Watermark from '../components/svg/Watermark'
import CodeMirror from '../lib/react-codemirror' import CodeMirror from '../lib/react-codemirror'
import { COLORS, LANGUAGE_MODE_HASH, LANGUAGE_NAME_HASH, DEFAULT_SETTINGS } from '../lib/constants' import { COLORS, LANGUAGE_MODE_HASH, LANGUAGE_NAME_HASH, DEFAULT_SETTINGS } from '../lib/constants'
const handleLanguageChange = (newCode, props) => {
if (props.config.language === 'auto') {
// try to set the language
const detectedLanguage = hljs.highlightAuto(newCode).language
const languageMode =
LANGUAGE_MODE_HASH[detectedLanguage] || LANGUAGE_NAME_HASH[detectedLanguage]
if (languageMode) {
return { language: languageMode.mime || languageMode.mode }
}
}
return { language: props.config.language }
}
class Carbon extends PureComponent { class Carbon extends PureComponent {
constructor(props) { constructor(props) {
super(props) super(props)
@ -19,9 +33,10 @@ class Carbon extends PureComponent {
language: props.config.language language: props.config.language
} }
this.handleLanguageChange = this.handleLanguageChange.bind(this)
this.handleTitleBarChange = this.handleTitleBarChange.bind(this) this.handleTitleBarChange = this.handleTitleBarChange.bind(this)
this.codeUpdated = this.codeUpdated.bind(this) this.codeUpdated = this.codeUpdated.bind(this)
this.onBeforeChange = this.onBeforeChange.bind(this)
this.handleLanguageChange = this.handleLanguageChange.bind(this)
} }
componentDidMount() { componentDidMount() {
@ -29,7 +44,7 @@ class Carbon extends PureComponent {
loading: false loading: false
}) })
this.handleLanguageChange(this.props.children) this.setState(handleLanguageChange(this.props.children, this.props))
const ro = new ResizeObserver(entries => { const ro = new ResizeObserver(entries => {
const cr = entries[0].contentRect const cr = entries[0].contentRect
@ -38,13 +53,12 @@ class Carbon extends PureComponent {
ro.observe(this.exportContainerNode) ro.observe(this.exportContainerNode)
} }
// TODO use getDerivedStateFromProps static getDerivedStateFromProps(newProps) {
UNSAFE_componentWillReceiveProps(newProps) { return handleLanguageChange(newProps.children, newProps) || null
this.handleLanguageChange(newProps.children, { customProps: newProps })
} }
codeUpdated(newCode) { codeUpdated(newCode) {
this.handleLanguageChange(newCode) this.handleLanguageChange(newCode, this.props)
this.props.updateCode(newCode) this.props.updateCode(newCode)
} }
@ -53,26 +67,15 @@ class Carbon extends PureComponent {
} }
handleLanguageChange = debounce( handleLanguageChange = debounce(
(newCode, config) => { (...args) => this.setState(handleLanguageChange(...args)),
const props = (config && config.customProps) || this.props
if (props.config.language === 'auto') {
// try to set the language
const detectedLanguage = hljs.highlightAuto(newCode).language
const languageMode =
LANGUAGE_MODE_HASH[detectedLanguage] || LANGUAGE_NAME_HASH[detectedLanguage]
if (languageMode) {
this.setState({ language: languageMode.mime || languageMode.mode })
}
} else {
this.setState({ language: props.config.language })
}
},
ms('300ms'), ms('300ms'),
{ trailing: true } { trailing: true }
) )
onBeforeChange(editor, meta, code) {
return this.codeUpdated(code)
}
render() { render() {
const config = { ...DEFAULT_SETTINGS, ...this.props.config } const config = { ...DEFAULT_SETTINGS, ...this.props.config }
const options = { const options = {
@ -115,7 +118,7 @@ class Carbon extends PureComponent {
) : null} ) : null}
<CodeMirror <CodeMirror
className={`CodeMirror__container window-theme__${config.windowTheme}`} className={`CodeMirror__container window-theme__${config.windowTheme}`}
onBeforeChange={(editor, meta, code) => this.codeUpdated(code)} onBeforeChange={this.onBeforeChange}
value={this.props.children} value={this.props.children}
options={options} options={options}
/> />

@ -45,21 +45,21 @@ export default class extends React.Component {
this.onDragEnd = this.onDragEnd.bind(this) this.onDragEnd = this.onDragEnd.bind(this)
} }
// TODO use getDerivedStateFromProps static getDerivedStateFromProps(nextProps, state) {
UNSAFE_componentWillReceiveProps(nextProps) { if (state.crop) {
if (this.state.crop && this.props.aspectRatio !== nextProps.aspectRatio) {
// update crop for editor container aspect-ratio change // update crop for editor container aspect-ratio change
this.setState({ return {
crop: makeAspectCrop( crop: makeAspectCrop(
{ {
...this.state.crop, ...state.crop,
aspect: nextProps.aspectRatio aspect: nextProps.aspectRatio
}, },
this.state.imageAspectRatio state.imageAspectRatio
) )
})
} }
} }
return null
}
async onDragEnd() { async onDragEnd() {
if (this.state.pixelCrop) { if (this.state.pixelCrop) {

Loading…
Cancel
Save