Skip to content

Fix(#7414): Fix React's bad setState() call warnings #7416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dev-test/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ collections: # A list of collections the CMS should be able to edit
summary: '{{title}} -- {{year}}/{{month}}/{{day}}'
create: true # Allow users to create new documents in this collection
editor:
visualEditing: true
visualEditing: false
view_filters:
- label: Posts With Index
field: title
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import styled from '@emotion/styled';
import { connect } from 'react-redux';
import { Link } from 'react-router-dom';
Expand Down Expand Up @@ -81,14 +81,24 @@ const CardBody = styled.div`
}
`;

const CardImage = styled.div`
const StyledImage = styled.div`
background-image: url(${props => props.src});
background-position: center center;
background-size: cover;
background-repeat: no-repeat;
height: 150px;
`;

function CardImage({ getAsset, value, field }) {
const [asset, setAsset] = useState(null);

useEffect(() => {
setAsset(value ? getAsset(value, field) : null);
}, [value, field, getAsset]);

return asset ? <StyledImage src={asset.toString()} /> : null;
}

function EntryCard({
path,
summary,
Expand Down Expand Up @@ -117,7 +127,7 @@ function EntryCard({
{collectionLabel ? <CollectionLabel>{collectionLabel}</CollectionLabel> : null}
<CardHeading>{summary}</CardHeading>
</CardBody>
{image ? <CardImage src={getAsset(image, imageField).toString()} /> : null}
{image ? <CardImage getAsset={getAsset} value={image} field={imageField} /> : null}
</GridCardLink>
</GridCard>
);
Expand Down
23 changes: 17 additions & 6 deletions packages/decap-cms-widget-file/src/withFileControl.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import ImmutablePropTypes from 'react-immutable-proptypes';
import styled from '@emotion/styled';
Expand Down Expand Up @@ -61,8 +61,19 @@ const StyledImage = styled.img`
object-fit: contain;
`;

function Image(props) {
return <StyledImage role="presentation" {...props} />;
function Image({ value, field, getAsset }) {
const [asset, setAsset] = useState(null);

useEffect(() => {
if (value) {
const newAsset = getAsset(value, field);
setAsset(newAsset);
} else {
setAsset(null);
}
}, [value, field, getAsset]);

return asset ? <StyledImage role="presentation" src={asset} /> : null;
}

function SortableImageButtons({ onRemove, onReplace }) {
Expand All @@ -89,7 +100,7 @@ function SortableImage(props) {
return (
<div ref={setNodeRef} style={style} {...attributes} {...listeners}>
<ImageWrapper sortable>
<Image src={getAsset(itemValue, field) || ''} />
<Image value={itemValue} field={field} getAsset={getAsset} />
</ImageWrapper>
<SortableImageButtons
item={itemValue}
Expand Down Expand Up @@ -412,6 +423,7 @@ export default function withFileControl({ forImage } = {}) {
renderImages = () => {
const { getAsset, value, field } = this.props;
const items = valueListToSortableArray(value);

if (isMultiple(value)) {
return (
<SortableMultiImageWrapper
Expand All @@ -428,10 +440,9 @@ export default function withFileControl({ forImage } = {}) {
);
}

const src = getAsset(value, field);
return (
<ImageWrapper>
<Image src={src || ''} />
<Image value={value} field={field} getAsset={getAsset} />
</ImageWrapper>
);
};
Expand Down
15 changes: 13 additions & 2 deletions packages/decap-cms-widget-image/src/ImagePreview.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import styled from '@emotion/styled';
import { List } from 'immutable';
Expand All @@ -11,7 +11,18 @@ const StyledImage = styled(({ src }) => <img src={src || ''} role="presentation"
`;

function StyledImageAsset({ getAsset, value, field }) {
return <StyledImage src={getAsset(value, field)} />;
const [asset, setAsset] = useState(null);

useEffect(() => {
if (value) {
const newAsset = getAsset(value, field);
setAsset(newAsset);
} else {
setAsset(null);
}
}, [value, field, getAsset]);

return asset ? <StyledImage role="presentation" src={asset} /> : null;
}

function ImagePreviewContent(props) {
Expand Down
4 changes: 2 additions & 2 deletions packages/decap-cms-widget-object/src/ObjectPreview.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import PropTypes from 'prop-types';
import ImmutablePropTypes from 'react-immutable-proptypes';
import { WidgetPreviewContainer } from 'decap-cms-ui-default';

function ObjectPreview({ field }) {
Expand All @@ -11,7 +11,7 @@ function ObjectPreview({ field }) {
}

ObjectPreview.propTypes = {
field: PropTypes.node,
field: ImmutablePropTypes.map,
};

export default ObjectPreview;