react-simplemde-editor
react-simplemde-editor copied to clipboard
custom actions do not have access to state... kinda
I am not sure if this is a bug or just my inexperience with react hooks. I've been using class components for a long time, and i am starting a new project and decided (finally) to jump into hooks.
The issue I am encountering is that I have a custom action that triggers after a button is clicked. But, for some reason, the function being called does not have access to the component's state. But if you change a bit of the code, and let it hot reload, the next time you click it does have access to state.
Steps to reproduce
- Go to https://codesandbox.io/s/zealous-pasteur-gys689?file=/src/App.js
- Open the console
- Click the Bold button
- Observe that the lineAndCursor returns null
- Go to the editor and add some spacing or make any non-breaking change
- Click the Bold button
- Observe that the lineAndCursor returns a value
Again, I am not sure if this is me still trying to wrap my head around hooks.
For context, I was trying to create a custom dropdown menu with the goal of adding blocks of text when clicked. I eventually figured out that the custom action passes the editor in the callback. So I eventually figured out that I could just editor.codemirror.replaceSelection("sometext");
. And that works fine. Regardless, I wanted to report this in case it is helpful. It is just such bizarre behavior that the issue resolves after the hot reload, but not on a fresh pageload.