为什么我的块只保存以前的状态而不保存当前状态?
Why is my block only saving the previous state and not the current one?
我为新的 wordpress 编辑器做了一个块,但我被卡住了。一切正常,除非我保存我的页面。发布的是块,但处于以前的状态。
例如:如果我专注于可编辑区域,添加一个词并按下发布,最新添加的内容将不会出现在负载中。虽然我在记录组件属性时完全可以看到它。如果我再按一次发布,它会正常工作。
在我看来,我做错了某种竞争条件或某些状态管理。我是 React 和 Gutenberg 的新手。
这是我的保存功能:
import { RichText } from '@wordpress/editor';
import classnames from 'classnames';
export default function save({ attributes }) {
const { align, figures } = attributes;
const className = classnames({ [ `has-text-align-${ align }` ]: align });
return (
<ul className={ className }>
{ figures.map( (figure, idx) => (
<li key={ idx }>
<RichText.Content tagName="h6" value={ figure.number } />
<RichText.Content tagName="p" value={ figure.description } />
</li>
) ) }
</ul>
);
}
我的编辑:
import { Component } from '@wordpress/element';
import { AlignmentToolbar, BlockControls, RichText } from '@wordpress/editor';
import { Toolbar } from '@wordpress/components';
import classnames from 'classnames';
import { normalizeEmptyRichText } from '../../utils/StringUtils';
import removeIcon from './remove-icon';
const MIN_FIGURES = 1;
const MAX_FIGURES = 4;
export default class FigureEdit extends Component {
constructor() {
super(...arguments);
this.state = {};
for(let idx = 0; idx < MAX_FIGURES; idx++){
this.state[`figures[${idx}].number`] = '';
this.state[`figures[${idx}].description`] = '';
}
}
onNumberChange(idx, number) {
const { attributes: { figures: figures }, setAttributes } = this.props;
figures[idx].number = normalizeEmptyRichText(number);
this.setState({ [`figures[${idx}].number`]: normalizeEmptyRichText(number) });
return setAttributes({ figures });
}
onDescriptionChange(idx, description) {
const { attributes: { figures: figures }, setAttributes } = this.props;
figures[idx].description = normalizeEmptyRichText(description);
this.setState({ [`figures[${idx}].description`]: normalizeEmptyRichText(description) });
return setAttributes({ figures });
}
addFigure() {
let figures = [...this.props.attributes.figures, {number:'', description:''}];
this.props.setAttributes({figures});
}
removeFigure() {
let figures = [...this.props.attributes.figures];
figures.pop();
this.resetFigureState(figures.length);
this.props.setAttributes({figures});
}
resetFigureState(idx) {
this.setState({
[`figures[${idx}].number`]: '',
[`figures[${idx}].description`]: ''
});
}
render() {
const {attributes, setAttributes, className} = this.props;
const { align, figures } = attributes;
const toolbarControls = [
{
icon: 'insert',
title: 'Add a figure',
isDisabled: this.props.attributes.figures.length >= MAX_FIGURES,
onClick: () => this.addFigure()
},
{
icon: removeIcon,
title: 'Remove a figure',
isDisabled: this.props.attributes.figures.length <= MIN_FIGURES,
onClick: () => this.removeFigure()
}
];
return (
<>
<BlockControls>
<Toolbar controls={ toolbarControls } />
<AlignmentToolbar
value={ align }
onChange={ align=>setAttributes({align}) }
/>
</BlockControls>
<ul className={ classnames(className, { [ `has-text-align-${ align }` ]: align }) }>
{ figures.map( (figure, idx) => (
<li key={ idx }>
<RichText
className="figure-number"
formattingControls={ [ 'link' ] }
value={ figure.number }
onChange={ number => this.onNumberChange(idx, number) }
placeholder={ !this.state[`figures[${idx}].number`].length ? '33%' : '' }
/>
<RichText
className="figure-description"
formattingControls={ [ 'link' ] }
value={ figure.description }
onChange={ description => this.onDescriptionChange(idx, description) }
placeholder={ !this.state[`figures[${idx}].description`].length ? 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor' : '' }
/>
</li>
) ) }
</ul>
</>
);
}
}
问题与不变性有关,在我的 onChange
方法中,我正在改变 figures
属性。看来你不能那样做。想要正确刷新道具,就得给一个新的对象。
我的更改处理程序现在看起来像这样:
onChange(idx, field, value) {
const figures = this.props.attributes.figures.slice();
figures[idx][field] = normalizeEmptyRichText(value);
return this.props.setAttributes({ figures });
}
我怀疑这也解决了占位符问题。现在占位符按预期工作,我可以删除所有状态和构造函数代码。这证实这与 setState
.
无关
我为新的 wordpress 编辑器做了一个块,但我被卡住了。一切正常,除非我保存我的页面。发布的是块,但处于以前的状态。
例如:如果我专注于可编辑区域,添加一个词并按下发布,最新添加的内容将不会出现在负载中。虽然我在记录组件属性时完全可以看到它。如果我再按一次发布,它会正常工作。
在我看来,我做错了某种竞争条件或某些状态管理。我是 React 和 Gutenberg 的新手。
这是我的保存功能:
import { RichText } from '@wordpress/editor';
import classnames from 'classnames';
export default function save({ attributes }) {
const { align, figures } = attributes;
const className = classnames({ [ `has-text-align-${ align }` ]: align });
return (
<ul className={ className }>
{ figures.map( (figure, idx) => (
<li key={ idx }>
<RichText.Content tagName="h6" value={ figure.number } />
<RichText.Content tagName="p" value={ figure.description } />
</li>
) ) }
</ul>
);
}
我的编辑:
import { Component } from '@wordpress/element';
import { AlignmentToolbar, BlockControls, RichText } from '@wordpress/editor';
import { Toolbar } from '@wordpress/components';
import classnames from 'classnames';
import { normalizeEmptyRichText } from '../../utils/StringUtils';
import removeIcon from './remove-icon';
const MIN_FIGURES = 1;
const MAX_FIGURES = 4;
export default class FigureEdit extends Component {
constructor() {
super(...arguments);
this.state = {};
for(let idx = 0; idx < MAX_FIGURES; idx++){
this.state[`figures[${idx}].number`] = '';
this.state[`figures[${idx}].description`] = '';
}
}
onNumberChange(idx, number) {
const { attributes: { figures: figures }, setAttributes } = this.props;
figures[idx].number = normalizeEmptyRichText(number);
this.setState({ [`figures[${idx}].number`]: normalizeEmptyRichText(number) });
return setAttributes({ figures });
}
onDescriptionChange(idx, description) {
const { attributes: { figures: figures }, setAttributes } = this.props;
figures[idx].description = normalizeEmptyRichText(description);
this.setState({ [`figures[${idx}].description`]: normalizeEmptyRichText(description) });
return setAttributes({ figures });
}
addFigure() {
let figures = [...this.props.attributes.figures, {number:'', description:''}];
this.props.setAttributes({figures});
}
removeFigure() {
let figures = [...this.props.attributes.figures];
figures.pop();
this.resetFigureState(figures.length);
this.props.setAttributes({figures});
}
resetFigureState(idx) {
this.setState({
[`figures[${idx}].number`]: '',
[`figures[${idx}].description`]: ''
});
}
render() {
const {attributes, setAttributes, className} = this.props;
const { align, figures } = attributes;
const toolbarControls = [
{
icon: 'insert',
title: 'Add a figure',
isDisabled: this.props.attributes.figures.length >= MAX_FIGURES,
onClick: () => this.addFigure()
},
{
icon: removeIcon,
title: 'Remove a figure',
isDisabled: this.props.attributes.figures.length <= MIN_FIGURES,
onClick: () => this.removeFigure()
}
];
return (
<>
<BlockControls>
<Toolbar controls={ toolbarControls } />
<AlignmentToolbar
value={ align }
onChange={ align=>setAttributes({align}) }
/>
</BlockControls>
<ul className={ classnames(className, { [ `has-text-align-${ align }` ]: align }) }>
{ figures.map( (figure, idx) => (
<li key={ idx }>
<RichText
className="figure-number"
formattingControls={ [ 'link' ] }
value={ figure.number }
onChange={ number => this.onNumberChange(idx, number) }
placeholder={ !this.state[`figures[${idx}].number`].length ? '33%' : '' }
/>
<RichText
className="figure-description"
formattingControls={ [ 'link' ] }
value={ figure.description }
onChange={ description => this.onDescriptionChange(idx, description) }
placeholder={ !this.state[`figures[${idx}].description`].length ? 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor' : '' }
/>
</li>
) ) }
</ul>
</>
);
}
}
问题与不变性有关,在我的 onChange
方法中,我正在改变 figures
属性。看来你不能那样做。想要正确刷新道具,就得给一个新的对象。
我的更改处理程序现在看起来像这样:
onChange(idx, field, value) {
const figures = this.props.attributes.figures.slice();
figures[idx][field] = normalizeEmptyRichText(value);
return this.props.setAttributes({ figures });
}
我怀疑这也解决了占位符问题。现在占位符按预期工作,我可以删除所有状态和构造函数代码。这证实这与 setState
.