我尝试使用收到的道具更新我的 CodeMirror 组件(从 react-codemirror 导入)的值,但即使道具发生了变化,该值也不会改变。而且因为我使用 corsanywhere 从 API 加载以克服 CORS 限制,所以有时获取请求会失败。但是,当 fetch 成功时,该值仍然没有更新。
这是我尝试的屏幕截图:
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} />;
}
这是我在输入中键入“hello world”时两个日志语句的控制台输出,证明道具已更新:
CodeConsole props: {output: {stdout: "hello world↵", stderr: "", error: ""}}
stdout: we
请参阅下面的答案以获取解决方案