最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Reactjs - what is the workflow to force a refresh? - Stack Overflow

programmeradmin2浏览0评论

I'm just trying to create a react ponent wrapping the CodeMirror (4.1) editor.

I came across this problem for which there is a workround via forcing a refresh once the ponent has loaded, but I'm not quite sure of the workflow I need to achieve this when react is added into the picture.

The suggestion is that to overe the error I would need to

"Call .refresh() after resizing the wrapping container."

My code is currently as follows in the Editor ponent:

  function ($, React, CodeMirror) {

    return React.createClass({

      render: function () {
        console.log("render-editarea");
        return (
          <textarea id="editarea">
-- Comment here
USE [All Data Items];
SELECT ID FROM [Test Event]
          </textarea>
        )
      },

      ponentDidMount: function () { 
        var onExecute = this.props.onExecute;
        var editorNode = document.getElementById("editarea");
        console.log("ponentDidUpdate-editarea:" + editorNode); 
        var editor = CodeMirror.fromTextArea(editorNode, {        
          lineNumbers: true,
          matchBrackets: true,
          indentUnit: 4,
          mode: "text/x-mssql",
          extraKeys: {"Ctrl-E": function(cm) {    
            console.log(editor.getValue());
            onExecute(editor.getValue());
          }}
        });
      },

and it is loaded via the Render function of the parent ponent

I have tried

  • hooking the window resize event (as shown in the React manual) in the editor ponent.
  • forcing a refresh in the parent ponent's ponentDidMount function using $("#editarea").refresh();

but neither of these appeared to work

So I'd be grateful if someone could show me the right way to do it.

Many thx

I'm just trying to create a react ponent wrapping the CodeMirror (4.1) editor.

I came across this problem for which there is a workround via forcing a refresh once the ponent has loaded, but I'm not quite sure of the workflow I need to achieve this when react is added into the picture.

The suggestion is that to overe the error I would need to

"Call .refresh() after resizing the wrapping container."

My code is currently as follows in the Editor ponent:

  function ($, React, CodeMirror) {

    return React.createClass({

      render: function () {
        console.log("render-editarea");
        return (
          <textarea id="editarea">
-- Comment here
USE [All Data Items];
SELECT ID FROM [Test Event]
          </textarea>
        )
      },

      ponentDidMount: function () { 
        var onExecute = this.props.onExecute;
        var editorNode = document.getElementById("editarea");
        console.log("ponentDidUpdate-editarea:" + editorNode); 
        var editor = CodeMirror.fromTextArea(editorNode, {        
          lineNumbers: true,
          matchBrackets: true,
          indentUnit: 4,
          mode: "text/x-mssql",
          extraKeys: {"Ctrl-E": function(cm) {    
            console.log(editor.getValue());
            onExecute(editor.getValue());
          }}
        });
      },

and it is loaded via the Render function of the parent ponent

I have tried

  • hooking the window resize event (as shown in the React manual) in the editor ponent.
  • forcing a refresh in the parent ponent's ponentDidMount function using $("#editarea").refresh();

but neither of these appeared to work

So I'd be grateful if someone could show me the right way to do it.

Many thx

Share Improve this question edited Jul 11, 2018 at 9:56 Aliaksandr Sushkevich 12.5k8 gold badges41 silver badges46 bronze badges asked May 19, 2014 at 16:59 Simon WoodsSimon Woods 2,2415 gold badges27 silver badges35 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

Use the ref attribute to reference rendered nodes rather than IDs or DOM selectors:

function ($, React, CodeMirror) {

  return React.createClass({

    render: function () {
      console.log("render-editarea");
      return (
        <textarea ref="editarea">
-- Comment here
USE [All Data Items];
SELECT ID FROM [Test Event]
        </textarea>
      )
    },

    ponentDidMount: function () { 
      var onExecute = this.props.onExecute;
      var editorNode = this.refs.editarea;
      console.log("ponentDidUpdate-editarea:" + editorNode); 
      var editor = CodeMirror.fromTextArea(editorNode, {        
        lineNumbers: true,
        matchBrackets: true,
        indentUnit: 4,
        mode: "text/x-mssql",
        extraKeys: {"Ctrl-E": function(cm) {    
          console.log(editor.getValue());
          onExecute(editor.getValue());
        }}
      });
    },

So this post helped me. The .refresh() was a function on CodeMirror which I hadn't fully understood. I used the method as suggested in that post in the parents ponentDidLoad event.

ponentDidMount: function () {              
  $('.CodeMirror').each(function(i, el){
    el.CodeMirror.refresh();
  });        
},
发布评论

评论列表(0)

  1. 暂无评论