Skip to content

chore: Remove refs from Rich Editor Data Field#4569

Merged
OliwiaGowor merged 4 commits intokyma-project:mainfrom
dbadura:fix-react-refs-4
Feb 5, 2026
Merged

chore: Remove refs from Rich Editor Data Field#4569
OliwiaGowor merged 4 commits intokyma-project:mainfrom
dbadura:fix-react-refs-4

Conversation

@dbadura
Copy link
Contributor

@dbadura dbadura commented Jan 30, 2026

Description

Changes proposed in this pull request:

  • Refactor RichEditorDataField to remove useEffect and refs

Related issue(s)
#4396

Definition of done

  • The PR's title starts with one of the following prefixes:
    • feat: A new feature
    • fix: A bug fix
    • docs: Documentation only changes
    • refactor: A code change that neither fixes a bug nor adds a feature
    • test: Adding tests
    • revert: Revert commit
    • chore: Maintainance changes to the build process or auxiliary tools, libraries, workflows, etc.
  • Related issues are linked. To link internal trackers, use the issue IDs like backlog#4567
  • Explain clearly why you created the PR and what changes it introduces
  • All necessary steps are delivered, for example, tests, documentation, merging


function createInternalState(data, previousInitialState) {
const dataAsArray = transformData(data, previousInitialState);
if (checkIfLastItemIsNotNull) {
Copy link
Contributor

@KonradPietocha KonradPietocha Feb 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this working correctly? You're not executing checkIfLastItemIsNotNull function here and not passing it any arguments. In such form it will be always true.

const [internalData, setInternalData] = useState([]);
const valueRef = useRef(null);
const firstRender = useRef(true); // detect languages only on first render
const [internalData, setInternalData] = useState(() =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this function execute here? Shouldn't it be: createInternalState(data, null) instead of: ()=>createInternalState(data, null)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this form the createInternalState will be called only on the initial render and the result will be saved, instead of calling the function on every render

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. Great thing. I read later that this is indeed very useful when the initial state is expensive to process.

@@ -53,15 +64,15 @@ export function RichEditorDataField({
key={index}
item={item}
setInternalData={setInternalData}
Copy link
Contributor

@KonradPietocha KonradPietocha Feb 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's strange but I can't see setInternalData prop in RichEditorSection component. Someone probably changed it at some point but left it.

Comment on lines -16 to -26
// update internal value
if (JSON.stringify(data) !== valueRef.current) {
setInternalData(
Object.entries(data || {}).map(([key, value]) => ({
key,
value,
language: internalData.find((d) => d?.key === key)?.language || '',
})),
);
firstRender.current = false;
valueRef.current = JSON.stringify(data);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would have been enough to put it in useEffect?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useEffect would trigger an extra render cycle here, React docs actually recommend this pattern over useEffect

@OliwiaGowor OliwiaGowor merged commit c5e8fa5 into kyma-project:main Feb 5, 2026
22 of 26 checks passed
@dbadura dbadura deleted the fix-react-refs-4 branch February 9, 2026 08:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants