Skip to content

Commit c599a6c

Browse files
committed
chore: Modify unsafe component lifecycles. #32
1 parent f851c21 commit c599a6c

File tree

4 files changed

+36
-27
lines changed

4 files changed

+36
-27
lines changed

src/components/CodeMirror/index.tsx

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,6 @@ export default class ReactCodeMirror extends Component<ICodeMirror, ICodeMirrorS
8181
// public editor!: Doc | Editor | EditorFromTextArea | Editor;
8282
public constructor(props: Readonly<ICodeMirror>) {
8383
super(props);
84-
}
85-
public render() {
86-
return (
87-
<textarea ref={(instance: HTMLTextAreaElement) => this.textarea = instance} />
88-
);
89-
}
90-
91-
public componentWillMount() {
9284
if (SERVER_RENDERED) {
9385
return;
9486
}
@@ -97,6 +89,11 @@ export default class ReactCodeMirror extends Component<ICodeMirror, ICodeMirrorS
9789
this.props.editorWillMount();
9890
}
9991
}
92+
public render() {
93+
return (
94+
<textarea ref={(instance: HTMLTextAreaElement) => this.textarea = instance} />
95+
);
96+
}
10097

10198
public componentDidMount() {
10299
if (SERVER_RENDERED) {
@@ -116,10 +113,9 @@ export default class ReactCodeMirror extends Component<ICodeMirror, ICodeMirrorS
116113

117114
this.renderCodeMirror(this.props);
118115
}
119-
public UNSAFE_componentWillReceiveProps(nextPros: ICodeMirror) {
120-
if (nextPros.value !== this.props.value || nextPros.width !== this.props.width || nextPros.height !== this.props.height) {
121-
this.renderCodeMirror(nextPros);
122-
}
116+
117+
componentDidUpdate() {
118+
this.renderCodeMirror(this.props);
123119
}
124120

125121
public shouldComponentUpdate(nextProps: ICodeMirror, nextState: ICodeMirrorState) {
@@ -128,7 +124,7 @@ export default class ReactCodeMirror extends Component<ICodeMirror, ICodeMirrorS
128124
|| nextProps.height !== this.props.height
129125
|| nextProps.width !== this.props.width;
130126
}
131-
// 将props中所有的事件处理函数映射并保存
127+
// 将 props 中所有的事件处理函数映射并保存
132128
public getEventHandleFromProps(): IEventDict {
133129
const propNames = Object.keys(this.props);
134130
const eventHandle = propNames.filter((prop) => {

src/components/PreviewMarkdown/index.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,18 @@ export default class PreviewMarkdown extends React.Component<IPreviewMarkdown, I
5050
public hide() {
5151
this.setState({ visible: false });
5252
}
53-
54-
public componentWillReceiveProps(nextProps: IPreviewMarkdown) {
55-
if (nextProps.visible !== this.props.visible) {
56-
this.setState({ visible: nextProps.visible });
53+
static getDerivedStateFromProps(props: IPreviewMarkdown, state: IPreviewMarkdownState) {
54+
if (props.value !== state.value) {
55+
return {
56+
value: props.value
57+
}
58+
}
59+
if (props.visible !== state.visible) {
60+
return {
61+
visible: props.visible
62+
}
5763
}
64+
return null;
5865
}
5966

6067
public highlight() {

src/index.tsx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default class MarkdownEditor extends React.PureComponent<IMarkdownEditor,
4949
visibleEditor={visibleEditor}
5050
ref={this.getInstance}
5151
{...codemirrorProps}
52-
onChange={this.onChange}
52+
onChange={this.onChange.bind(this)}
5353
/>
5454
)}
5555
<PreviewMarkdown
@@ -84,12 +84,9 @@ export default class MarkdownEditor extends React.PureComponent<IMarkdownEditor,
8484
this.setEditorSize(isFullScreen);
8585
}
8686
}
87-
88-
public componentWillReceiveProps(nextProps: IMarkdownEditor) {
89-
if (nextProps.visible !== this.props.visible) {
90-
nextProps.visible ? this.preview.show() : this.preview.hide();
91-
this.CodeMirror.editor.setSize(nextProps.visible ? '50%' : '100%');
92-
}
87+
public componentDidUpdate() {
88+
this.props.visible ? this.preview.show() : this.preview.hide();
89+
this.CodeMirror.editor.setSize(this.props.visible ? '50%' : '100%');
9390
}
9491

9592
public getInstance = (editor: CodeMirror) => {
@@ -100,9 +97,6 @@ export default class MarkdownEditor extends React.PureComponent<IMarkdownEditor,
10097
private onChange = (editor: IInstance, data: CodeMirror.EditorChange, value: string) => {
10198
const { onChange } = this.props as IMarkdownEditor;
10299
if (onChange) {
103-
if (this.preview) {
104-
this.preview.updateSource(editor.getValue());
105-
}
106100
onChange(editor, data, editor.getValue());
107101
}
108102
}

website/App.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ const DocumentStrSource = DocumentStr.replace(/([\s\S]*)<!--dividing-->/, '').re
1212

1313
interface IAppState {
1414
mdstr: string;
15+
visible: boolean;
1516
}
1617

18+
let count = 1;
19+
1720
export default class App extends React.Component<any, IAppState> {
1821
constructor(props: any) {
1922
super(props);
2023
this.state = {
2124
mdstr: DocumentStrSource,
25+
visible: true,
2226
}
2327
}
2428
public render() {
@@ -29,7 +33,15 @@ export default class App extends React.Component<any, IAppState> {
2933
<Logo />
3034
</div>
3135
<div className={styles.editor}>
36+
<button onClick={() => {
37+
count += 1;
38+
this.setState({ mdstr: `String ${count}` });
39+
}}>Modify Markdown</button>
40+
<button onClick={() => {
41+
this.setState({ visible: !this.state.visible })
42+
}}>{this.state.visible ? 'Show' : 'Hide'}</button>
3243
<MarkdownEditor
44+
visible={this.state.visible}
3345
options={{
3446
autofocus: true,
3547
showCursorWhenSelecting: true,

0 commit comments

Comments
 (0)