diff --git a/packages/editor/src/components/post-text-editor/index.js b/packages/editor/src/components/post-text-editor/index.js index 55bcb7228bf01..b4e380190692a 100644 --- a/packages/editor/src/components/post-text-editor/index.js +++ b/packages/editor/src/components/post-text-editor/index.js @@ -7,7 +7,7 @@ import Textarea from 'react-autosize-textarea'; * WordPress dependencies */ import { __ } from '@wordpress/i18n'; -import { useState } from '@wordpress/element'; +import { useEffect, useState, useRef } from '@wordpress/element'; import { parse } from '@wordpress/blocks'; import { useDispatch, useSelect } from '@wordpress/data'; import { useInstanceId } from '@wordpress/compose'; @@ -29,6 +29,7 @@ export default function PostTextEditor() { const [ value, setValue ] = useState( postContent ); const [ isDirty, setIsDirty ] = useState( false ); const instanceId = useInstanceId( PostTextEditor ); + const valueRef = useRef(); if ( ! isDirty && value !== postContent ) { setValue( postContent ); @@ -65,6 +66,18 @@ export default function PostTextEditor() { } }; + useEffect( () => { + valueRef.current = value; + }, [ value ] ); + + // Ensure changes aren't lost when component unmounts. + useEffect( () => { + return () => { + const blocks = parse( valueRef.current ); + resetEditorBlocks( blocks ); + }; + }, [] ); + return ( <>