react-codemirror: Code mirror value doesn't update with state change in v1.0.0
Changing props.value does not re-render code mirror. This is a new issue in v1.0.0, previously this worked. Changing props.value via the onChange event works fine, but programmatically changing props.value does not call a re-render.
This can be reproduced with the following code.
class Editor extends Component {
constructor() {
super();
this.state = { value: 'abc' };
}
render() {
const { value } = this.state;
console.log(value);
return (
<div>
<CodeMirror value={value} />
<button onClick={() => this.setState({ value: 'def' })}>Click to change value</button>
</div>
);
}
}
This appears like it may have something to do with this change listed in the history file: fixed; Only updates the CodeMirror value if props.value has changed.
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 59
- Comments: 27 (3 by maintainers)
Commits related to this issue
- WIP Edit scripts returned from the server Had to downgrade react-codemirror due to a bug. Issues and PRs are open to resolve: - https://github.com/JedWatson/react-codemirror/issues/121 - https://git... — committed to influxdata/chronograf by 121watts 7 years ago
- Merge pull request #1 from skidding/106-fix-update No longer debounce componentWillReceiveProps #106 — committed to Dentosal/react-codemirror by Dentosal 7 years ago
- WIP Edit scripts returned from the server Had to downgrade react-codemirror due to a bug. Issues and PRs are open to resolve: - https://github.com/JedWatson/react-codemirror/issues/121 - https://git... — committed to influxdata/chronograf by 121watts 7 years ago
- WIP Edit scripts returned from the server Had to downgrade react-codemirror due to a bug. Issues and PRs are open to resolve: - https://github.com/JedWatson/react-codemirror/issues/121 - https://git... — committed to influxdata/chronograf by 121watts 7 years ago
- No longer debounce componentWillReceiveProps #106 — committed to cpunion/react-codemirror by ovidiuch 7 years ago
- WIP Edit scripts returned from the server Had to downgrade react-codemirror due to a bug. Issues and PRs are open to resolve: - https://github.com/JedWatson/react-codemirror/issues/121 - https://git... — committed to influxdata/chronograf by 121watts 7 years ago
- WIP Edit scripts returned from the server Had to downgrade react-codemirror due to a bug. Issues and PRs are open to resolve: - https://github.com/JedWatson/react-codemirror/issues/121 - https://git... — committed to influxdata/chronograf by 121watts 7 years ago
- Add ReactCodemirror at v0.3.0 Later versions have a problem updating: https://github.com/JedWatson/react-codemirror/issues/106 — committed to Exabyte-io/materials-designer by timurbazhirov 5 years ago
- Add ReactCodemirror at v0.3.0 Later versions have a problem updating: https://github.com/JedWatson/react-codemirror/issues/106 — committed to Exabyte-io/materials-designer by timurbazhirov 5 years ago
PS. Try using referencing my branch in your package.json (temporarily!) to see it solves your issues as well:
Update: You might want to use https://github.com/scniro/react-codemirror2
+1 whats going on with this? Merging the PR fix would be soooooper
Also, should anyone find this useful I threw up a super quick package on npm => react-codemirror2. If others find this useful I’ll be happy to maintain it moving forward and build it up however we see fit.
@MrSauceman Of course. It’s pretty bare bones, but here is what does the trick for me with some sample options…
wrapper
usage
where
valueandoptionscan be passed as a living prop from the parent componentAnother work around: add a
keyprop to the codemirror component. Thenkey = key + 1when thevalueprop changes. This makes the editor re-mount. It’s ok for me because thevalueprop only changes when the user clicks the ‘reset’ button.FWIW I tried it on a fresh project and couldn’t get that feature to work. I switched to Ace editor which does the trick for me.
I’m not putting that comment to be dismissive or aggressive but as someone who doesn’t know these libraries very well it took me a while to find out so here’s a link: https://github.com/securingsincity/react-ace
I’m having this issue as well, gradly I’ve found this post! I’ll try the solutions here mentioned but it would be really awesome to get this merged soon!
Edit: Please try https://github.com/scniro/react-codemirror2 instead. That fork is maintained, available via npm and gathering stars.
~I can confirm that
@skidding/react-codemirrorworks. Newbies guide:~import CodeMirror from 'react-codemirror';withimport CodeMirror from '@skidding/react-codemirror';~package-lock.json(there are probably better ways)~package.jsonreplace"react-codemirror": "^1.0.0",with"@skidding/react-codemirror": "^1.0.0",~npm install, run webpack/wait for compile/reload bowser~Bump! I’m having this issue too!
@skidding you version of codemirror worked perfectly! yay. we were having so many issues with this. We were able to roll back to 0.2.6, and it worked, but then when we moved our state into a redux store, and codemirror stopped updating 😦.
Fortunately when using
import CodeMirror from '@skidding/react-codemirror';Everything worked perfectly!! For those curious…In the end it was simple as this:
, but it only worked using that library I referenced earlier. Hopefully that pull request will fix the issue, please update here when it has been merged.
For anyone who’s still hung up on this or considering another editor, I’d highly recommend giving the raw codemirror library a look to use directly. I’m doing so with a tiny component wrapper I rolled and it’s very straightforward. One less dep too (lodash as well)…
Edit: Please try https://github.com/scniro/react-codemirror2 instead. That fork is maintained, available via npm and gathering stars.
Published my fork under @skidding/react-codemirror until #107 gets merged or resolved otherwise.
Update: You might want to use https://github.com/scniro/react-codemirror2
@MartinHaeusler We ran into that issue too with the v1 update, introduced by this commit https://github.com/JedWatson/react-codemirror/commit/339b5975724cde338301f4c6842f2d52b4773e76. Your best bet is to either…
Bummer that we had abandon this project but supporting your own wrapper is actually not too difficult.