question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

setState in onChange will block the editor

See original GitHub issue
  onChange(newValue) {
    this.setState({ code: newValue });
    console.log('change', this.state.code);
  }

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:13
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

17reactions
brendanmcgiverncommented, May 5, 2017

@mbrochstein I was able to find a workaround by using Reacts shouldComponentUpdate method.

shouldComponentUpdate(nextProps, nextState) {
    if (this.state.aceEditorValue !== nextState.aceEditorValue) {
      return false
    } else {
      return true;
    }
  }

Then for my onChange method for the <AceEditor />

onChange = (newValue) => {
    this.setState({
      aceEditorValue: newValue
    });
  }

This will update the state of aceEditorValue with whatever is entered into the editor without re-rendering the entire component. Works for my use case.

3reactions
PavelPolyakovcommented, Nov 22, 2018

Well, this is a huge bug and it is still there. I tried to use react-ace with https://github.com/jaredpalmer/formik and behaviour is the same, it just stucks.

<AceEditor
mode="yaml"
theme="solarized_light"
onChange={value => {
  setFieldValue("content", value);
}}
tabSize={2}
editorProps={{ $blockScrolling: true }}
value={''}
width="100%"
/>

upd This looks not nice, but it worked out for me. The key issue, as I understand, is that you better not set state or do anything with the value right from the ace’s onChange (inline). You better do it in the different component’s function.

import React, { Component } from "react";
import { Formik, Form, Field } from "formik";

class AcePlusFormik extends Component {
  state = { content: "" };

  /**
   * Special treatment for ace content
   * @param {string} value
   */
  setContent(value) {
    this.setState({ content: value });
    this.setFieldValue("content", value);
  }

  render() {
    <Formik
      initialValues={{ title: "", content: "" }}
      onSubmit={values => console.log(values)}
    >
      {({ touched, setFieldValue }) => {
        this.setFieldValue = setFieldValue;
        return (
          <Form>
            <Field name="content">
              {({ field }) => (
                <React.Fragment>
                  <AceEditor
                    mode="yaml"
                    theme="solarized_light"
                    onChange={value => {
                      this.setContent(value);
                    }}
                    tabSize={2}
                    editorProps={{ $blockScrolling: true }}
                    value={this.state.content}
                    width="100%"
                  />
                  <input type="hidden" {...field} />
                </React.Fragment>
              )}
            </Field>
            <button
              className="button is-primary is-pulled-right"
              style={{ marginTop: "10px" }}
              type="submit"
            >
              Save
            </button>
          </Form>
        );
      }}
    </Formik>;
  }
}

Read more comments on GitHub >

github_iconTop Results From Across the Web

Calling setAttributes() in Gutenberg Block causes bad setState ...
This is the first time I have attempted to use setAttributes() outside of an onChange property in another component/block. The attribute name is...
Read more >
Using AceEditor in React - Medium
I was frustrated when using setState in AceEditor's onchange causes anything typed in the editor to not show at all — see the...
Read more >
React conditional rendering: 9 methods with examples
In this tutorial, we'll cover the most popular ways to implement conditional rendering in React, also reviewing some tips and best practices. To ......
Read more >
Updating Objects in State - React Docs
State can hold any kind of JavaScript value, including objects. But you shouldn't change objects that you hold in the React state directly....
Read more >
How to Edit Todos Items in a React Application | Ibaslogic
We will change this to true using the setState() method once we double click any of the items. We will also use the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found