question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

shouldNodeComponentUpdate doesnt catch document.data update

See original GitHub issue

Do you want to request a feature or report a bug?

Potential bug

What’s the current behavior?

I have some global settings in slate object which I keep in document.data. Some nodes depend on those settings and should rerender on change.

I implemented the following function to make it rerender:

shouldNodeComponentUpdate: (previousEditorProps, editorProps) => {
    if (
      previousEditorProps.editor.value.document.data.get('variables') !==
      editorProps.editor.value.document.data.get('variables')
    ) {
      console.log('variable changed, lets rerender');
      return true;
    }

    return undefined;
  },

The problem is that this function will never be called with old and new variables, on change previousEditorProps already contains new variables. Is it intended behaviour or a bug? If this is a bug, I will make some interactive example.

What’s the expected behavior?

previousEditorProps should be called once with old and new variables which would allow me to rerender all custom nodes.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:15 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
alanchrtcommented, Nov 3, 2018

@ianstormtaylor I didn’t hang onto my shouldNodeComponentUpdate implementation for anything since it never really solved my use case, but here’s an example (among several) of where I’m now using context to update components when relevant data changes (without something that normally triggers a re-render).

Here, BlockContext passes its consumers a beginKey, which is set to the key of the beginning delimiter’s text node for the currently-focused “greater block” (a blockquote, code block, etc.)

My BeginKeyword component switches to the non-Empty font (text becomes visible) whenever the related greater block is focused, then switches back to Empty if focus moves to another area outside the block.

You can see it in action by focusing the QUOTE or SRC block here (hard refresh if you visited before).

import React from 'react';
import { css, cx } from 'react-emotion';

import { BlockContext } from '../../../context';

const style = css`
  font-family: 'Empty';
  line-height: 16px;
`;

const focusedStyle = css`
  font-family: 'Roboto Mono';
`;

const BeginKeyword = ({ isFocused, attributes, children }) => (
  <span {...attributes} className={cx(style, { [focusedStyle]: isFocused })}>
    {children}
  </span>
);

const ContextWrapper = (props) => (
  <BlockContext.Consumer>
    {({ beginKey }) => {
       const { node } = props;
       const isFocused = (node.key === beginKey);
       return <BeginKeyword {...{ isFocused, ...props }} />
     }}
  </BlockContext.Consumer>
);

export default ContextWrapper;

Before, I would have tried using editor.props to pass beginKey into shouldNodeComponentUpdate, then compare to the previous value and see if it had changed. I would re-render all relevant nodes when it changed. In the case of the one above, it would determine if beginKey matched during the render. This never really worked as hoped since the previous editor.props weren’t stored.

1reaction
ianstormtaylorcommented, Nov 2, 2018

Can someone post code of what they were using shouldNodeComponentUpdate for before? and the new way they are handling it with context?

Read more comments on GitHub >

github_iconTop Results From Across the Web

shouldComponentUpdate() is not being called - Stack Overflow
stringify(nextProps)); but component still renders itself again. So I've tried just to return false (like do not ever update) but it still does....
Read more >
Update shouldComponentUpdate docs with advice about ...
The presence of an component with shouldComponentUpdate:false is blockin. ... to continue updating after the first "Toggle Top Data" click.
Read more >
React.Component
Use shouldComponentUpdate() to let React know if a component's output is not affected by the current change in state or props. The default...
Read more >
How and when to force a React component to re-render
React automatically re-renders components, but what happens when a component is not updating as expected? Find out what you can do here.
Read more >
Optimizing Performance - React
If you know that in some situations your component doesn't need to update, you can return false from shouldComponentUpdate instead, to skip the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found