简体   繁体   English

在React功能组件中更改道具时,CodeMirror值未更新

[英]CodeMirror Value not updating when props changed in React functional component

I tried to update the value of my CodeMirror Component (imported from react-codemirror) using props received but the value doesn't change even though the props changed. 我尝试使用收到的道具更新我的CodeMirror组件(从react-codemirror导入)的值,但是即使道具发生了变化,该值也不会发生变化。 And because I'm loading from an API using corsanywhere to overcome CORS restrictions, sometimes the fetch request fails. 而且由于我是使用corsanywhere从API加载来克服CORS限制的,所以有时提取请求失败。 However, when fetch succeeded, the value is still not updated. 但是,成功获取后,该值仍不会更新。

Here's a screenshot of my attempt: 这是我的尝试的屏幕截图: 尝试

function CodeConsole(props) {
  console.log("CodeConsole props: ", props);
  const options = {
    readOnly: true
  };
  const stdout = props.output.stdout;
  console.log("stdout: " + stdout);
  return <Codemirror value={stdout} options={options} autoFocus={false} />;
}

Here's the console output from the two log statements when I typed "hello world" into the input which proves that the props are updated: 这是当我在输入中输入“ hello world”时两个日志语句的控制台输出,以证明道具已更新:

CodeConsole props:  {output: {stdout: "hello world↵", stderr: "", error: ""}}
stdout: we

See my answer below for the solution 请参阅下面的答案以获取解决方案

I solved the problem by replace CodeMirror component with a regular textarea which updates its value accordingly. 我通过用常规的textarea替换CodeMirror组件来解决了这个问题,它会相应地更新其值。 However, I would still appreciate someone explain the reasons behind CodeMirror value's update issue. 但是,我仍然希望有人解释CodeMirror值更新问题的原因。 I'm looking for a consistent look and style for my code editor and console so using CodeMirror for the console is still very desirable. 我正在为代码编辑器和控制台寻找一致的外观和样式,因此仍然非常希望将CodeMirror用于控制台。

Update 更新资料

The problem is actually in the react-codemirror program. 问题实际上出在react-codemirror程序中。 I'm able to use code mirror in react again using a newer and well-maintained react-codemirror2 . 我能够使用更新且维护良好的react-codemirror2再次使用代码镜像。 See this GitHub issue for more details. 有关更多详细信息,请参见此GitHub问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM