react-native-code-editor icon indicating copy to clipboard operation
react-native-code-editor copied to clipboard

How do I get the CodeEditor value without causing issues

Open HarunKaranja opened this issue 3 years ago • 2 comments

I have realized that I can't use the state to get the value... I asked a question about 2 weeks ago and got no response.

Please anyone with a solution or working example I will appreciate

HarunKaranja avatar Jul 31 '22 08:07 HarunKaranja

You can simply use useState to get the CodeEditor value:

const [code, setCode] = useState < string > ('');
<SafeAreaView>
  <CodeEditor style={{ ...{ fontSize: 18, inputLineHeight: 18, highlighterLineHeight: 18, }, }} language={'cpp'}
    onChange={(data) => { setCode(data); }}
    syntaxStyle={CodeEditorSyntaxStyles.atomOneDark}
    showLineNumbers
  />
</SafeAreaView>

AdnanNuruddin avatar Oct 01 '23 05:10 AdnanNuruddin

I've made a patch-package that converts this into a controlled component. It's a bit hacky but what it does is that it replaces initialValue with value but since there's already a value on CodeEditor.tsx then I imported it as { value = initialValue } and then pass on the real 'value' to the onChange event which might not be the best approach but it does the trick for me

jdmg94 avatar Jan 17 '24 23:01 jdmg94