Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

State is equal to it's default value on commands exec function call #1846

Open
crva opened this issue Dec 8, 2023 · 2 comments
Open

State is equal to it's default value on commands exec function call #1846

crva opened this issue Dec 8, 2023 · 2 comments

Comments

@crva
Copy link

crva commented Dec 8, 2023

Problem

I'm trying to execute a function when a user press Command+Enter but my parent state value became it's default value when I use exec from the editor commands array.

The part that I don't understand is that when I execute my function from a button it works, my state is correct.

Sample code to reproduce your issue

EditorParent.jsx

const [value, setValue] useState('' as string);

const doSomething = () => {
  handle(value) // value will be `''` if I use the `exec` method from the ace editor, but the correctly set value when executed from the button. 
}

return <Editor value={value} setValue={setValue} doSomething={doSomething} />

Editor.jsx

function Editor({ value, setValue, doSomething }) {
      <AceEditor
        // ...
        onChange=(newValue => setValue(newValue))
        value={value}
        commands={[
          {
            name: 'doSomething',
            bindKey: { win: 'Ctrl-Enter', mac: 'Command-Enter' },
            exec: () =>
              doSomething(), // value will be `''`
          },
        ]}
      />
      <Button
        onClick={() => doSomething()} // value will be correct
      >
        Execute
      </Button>
}
@ddt313
Copy link

ddt313 commented Dec 25, 2023

same issue

@ddt313
Copy link

ddt313 commented Dec 26, 2023

Check the workaround here: #684

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants