Skip to main content
docs/start/quick-start

Quick start

A simple screen with the editor, a ref, onChange, a language and a theme.

Edit this page

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: 1 in a column, or an explicit height.
  • initialValue sets the initial text of the editor. It is read once, so use setText() to replace the document later.
  • language is the id of an installed language package. Without the package the editor shows plain text.
  • theme defaults to githubLight. The themes guide has the other two and shows how to write your own.
  • onChange tells you what changed in each edit. onChangeText gives you the whole text after every change, so keep it for small documents.
  • The ref's readers, such as getText(), getSelection() and getCursorRect(), return right away. Its writers, such as setText(), replaceRange() and undo(), return a promise. You can call all of them once onReady fires.

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.