Quick start
A simple screen with the editor, a ref, onChange, a language and a theme.
This is a simple CodePad usage: one screen with the editor, an undo button on the ref and a status line fed by onChange.
EditorScreen.tsx
import { useRef, useState } from 'react';
import { Button, StyleSheet, Text, View } from 'react-native';
import { CodeEditor, type CodeEditorRef, githubDark } from 'react-native-codepad';
const INITIAL = ['export function greet(name) {', ' return `Hello, ${name}`;', '}'].join('\n');
// Object props are compared by reference, so define them outside the component.
const INSETS = { bottom: 24 };
export function EditorScreen() {
const editor = useRef<CodeEditorRef>(null);
const [revision, setRevision] = useState(0);
return (
<View style={styles.screen}>
<View style={styles.bar}>
<Text style={styles.status}>revision {revision}</Text>
<Button title="Undo" onPress={() => editor.current?.undo()} />
<Button title="Log" onPress={() => console.log(editor.current?.getText())} />
</View>
<CodeEditor
ref={editor}
style={styles.editor}
initialValue={INITIAL}
language="javascript"
theme={githubDark}
contentInsets={INSETS}
onReady={() => console.log('lines', editor.current?.getLineCount())}
onChange={(e) => setRevision(e.revision)}
/>
</View>
);
}
const styles = StyleSheet.create({
screen: { flex: 1, backgroundColor: '#0d1117' },
bar: { flexDirection: 'row', alignItems: 'center', gap: 8, padding: 8 },
status: { color: '#8b949e', flex: 1 },
editor: { flex: 1 },
});
- The editor needs a height:
flex: 1in a column, or an explicitheight. initialValuesets the initial text of the editor. It is read once, so usesetText()to replace the document later.languageis the id of an installed language package. Without the package the editor shows plain text.themedefaults togithubLight. The themes guide has the other two and shows how to write your own.onChangetells you what changed in each edit.onChangeTextgives you the whole text after every change, so keep it for small documents.- The ref's readers, such as
getText(),getSelection()andgetCursorRect(), return right away. Its writers, such assetText(),replaceRange()andundo(), return a promise. You can call all of them onceonReadyfires.
Offsets count UTF-16 code units. Lines and columns start at 0, and ranges include the start but exclude the end, just like JavaScript strings.