Skip to content

React Native

Core, every adapter and the timers package run in React Native unchanged. Stores, async strategies, machines and bindings don’t depend on the DOM. The one difference is how actions read input: React Native callbacks pass values, not DOM events.

Terminal window
npx expo install @input-actions/core

Metro loads the packages with no configuration on React Native 0.79 or later (Expo SDK 53 or later), where it reads a package’s exports map by default. On older versions, turn that on in metro.config.js:

const { getDefaultConfig } = require('expo/metro-config');
const config = getDefaultConfig(__dirname);
config.resolver.unstable_enablePackageExports = true;
module.exports = config;

The 'use client' directives in core are for Next.js; Metro ignores them.

On the web, onChange receives an event and actions read event/target.value. React Native’s TextInput has onChangeText(text) and Switch has onValueChange(value), which pass the value itself. Read it with event/arg, or event/arg.N for the Nth argument:

import { Switch, Text, TextInput, View } from 'react-native';
import { useLocalActions, save } from '@input-actions/core';
function Profile() {
const { state, actions } = useLocalActions({ name: '', newsletter: false });
return (
<View>
<TextInput
value={state.name}
onChangeText={actions(save(['name'], 'event/arg'))}
/>
<Switch
value={state.newsletter}
onValueChange={actions(save(['newsletter'], 'event/arg'))}
/>
<Text>{state.name}</Text>
</View>
);
}

The same works in bindings and async actions: asyncDebounce('search', 'search', 300, 'event/arg') passes the typed text to the handler.

These read fields that only DOM events have. In React Native they give undefined, or do nothing:

  • Placeholders: event/target.* (value, checked, selectedIndex, files, dataset), event/key, event/keyCode, event/clientX and event/clientY.
  • Helpers built on them: saveInput, saveCheckbox, saveFiles, validateOnBlur, autoSave and searchAsYouType. Write the tuple with event/arg instead, for example save(path, 'event/arg') in place of saveInput(path).
  • preventDefault() and stopPropagation() have no effect.

The packages are ES modules only, and jest-expo doesn’t transform node_modules by default. Add @input-actions to the packages it transforms:

"jest": {
"preset": "jest-expo",
"transformIgnorePatterns": [
"node_modules/(?!((jest-)?react-native|@react-native(-community)?|expo(nent)?|@expo(nent)?/.*|@input-actions/.*))"
]
}

Then test with React Native Testing Library:

import { fireEvent, render, screen } from '@testing-library/react-native';
test('saves the name', async () => {
await render(<Profile />);
await fireEvent.changeText(screen.getByDisplayValue(''), 'walter');
expect(screen.getByText('walter')).toBeTruthy();
});

Every change runs a React Native consumer built from the packed packages, on Expo SDK 58 and React Native 0.88. It renders an app using useLocalActions, a Zustand store and useClock under jest-expo, fires onChangeText, onValueChange and onPress, then bundles the app for Android with Metro and Hermes.