我有一个通过npm导入的组件(CKEditor),它只关心其父组件在挂载时的状态。也就是说,无论父组件的状态发生了什么变化,如果CKEditor已经挂载,它都不会反映这些变化。
对我来说,这是个问题,因为当父组件更改其语言属性时,我需要让CKEditor基于父组件的状态进行更改。
有没有办法让我从父组件中卸载并重新挂载子组件?例如,是否可以根据父组件的“componentWillReceiveProps”卸载并重新挂载子组件?
对我来说,这是个问题,因为当父组件更改其语言属性时,我需要让CKEditor基于父组件的状态进行更改。
有没有办法让我从父组件中卸载并重新挂载子组件?例如,是否可以根据父组件的“componentWillReceiveProps”卸载并重新挂载子组件?
import React from 'react';
import CKEditor from "react-ckeditor-component";
export default class EditParagraph extends React.Component {
constructor(props) {
super(props)
this.state = {
// an object that has a different html string for each potential language
content: this.props.specs.content,
}
this.handleRTEChange = this.handleRTEChange.bind(this)
this.handleRTEBlur = this.handleRTEBlur.bind(this)
}
/**
* Native React method
* that runs every time the component is about to receive new props.
*/
componentWillReceiveProps(nextProps) {
const languageChanged = this.props.local.use_lang != nextProps.local.use_lang;
if (languageChanged) {
// how do I unmount the CKEditor and remount it ???
console.log('use_lang changed');
}
}
handleRTEChange(evt) {
// keeps track of changes within the correct language section
}
handleRTEBlur() {
// fully updates the specs only on Blur
}
getContent () {
// gets content relative to current use language
}
render() {
const content = this.getContent();
// This is logging the content relative to the current language (as expected),
// but CKEditor doesn't show any changes when content changes.
console.log('content:', content);
// I need to find a way of unmounting and re-mounting CKEditor whenever use_lang changes.
return (
<div>
<CKEditor
content={content}
events={{
"blur": this.handleRTEBlur,
"change": this.handleRTEChange
}}
/>
</div>
)
}
}
key={JSON.stringify(this.props.defaultCenter) + this.props.defaultZoom}
- Dorian