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.

Unable to get previous state in onChange plugin

See original GitHub issue

I would like to create a plugin to save the editor content when onChange is fired. Following documentation, I would like to check previous document value before saving to database with:

if (prevValue.document !== newValue.document) {
  // Save to database
}

But I can’t find a way to get my previous value from the onChange plugin handler:

const SavePlugin = () => ({
  onChange: ({ value: newValue }, next) => {
    // How can I get previousValue ??
    if (prevValue.document !== newValue.document) {
      const content = JSON.stringify(newValue.toJSON());
      localStorage.setItem('content', content);
    }

    next();
  },
});

Is there a way to get the previous state value from the onChange plugin event handler?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
gdehmlowcommented, Feb 20, 2019

I think this is a question better suited for the Slack channel (https://slate-slack.herokuapp.com/).

But you could, say, leverage the closure of the SavePlugin like so:

const SavePlugin = () => {
  let previousValue = null;
  return {
    onChange: ({ value: newValue }, next) => {
      // How can I get previousValue ??
      if (previousValue.document !== newValue.document) {
        const content = JSON.stringify(newValue.toJSON());
        localStorage.setItem('content', content);
     }
     previousValue = newValue;
     next();
    },
  };
};
0reactions
damien-monnicommented, Feb 21, 2019

You can close the issue (y)

Read more comments on GitHub >

github_iconTop Results From Across the Web

React state clearing onChange [duplicate] - Stack Overflow
React setState functions are asynchronous in nature so the console.log showing previous values might be due to the fact that the state ......
Read more >
onChange doesn't fire if input re-renders due to a setState() in ...
I have a separate use case reproducing the same issue, since react makes batch updates to state once per event handler so if...
Read more >
react-phone-number-input - npm
For example, if a user chooses "United States" and enters (213) 373-4253 in the input field then onChange(value) will be called with value...
Read more >
jQuery API Documentation
A factory function that returns a chainable utility object with methods to register multiple callbacks into callback queues, invoke callback queues, and relay ......
Read more >
The React Handbook – Learn React for Beginners
If you already have a React app installed using an older version of React, ... We can't assign a different literal to 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