react-native-code-editor
react-native-code-editor copied to clipboard
How do I get the CodeEditor value without causing issues
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
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>
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