Installation
Basic Usage
The most common way to use actions is with thefn() function from storybook/test, which creates a mock function that logs calls to the Actions panel:
Button.stories.tsx
- The event name (
onPress) - Arguments passed to the callback
- Timestamp of the interaction
Logging Multiple Actions
Components often have multiple event handlers. You can log all of them:Form.stories.tsx
Actions with Event Data
Actions automatically capture and display event arguments. This is useful for inspecting event payloads:TextInput.stories.tsx
onChangeText call will appear in the Actions panel showing the new text value.
Reusing Actions Across Stories
Define actions once in the meta object to reuse across all stories:Button.stories.tsx
Complete Example
Here’s a comprehensive example combining actions with other addons:ActionButton.stories.tsx
Actions Panel
The Actions panel displays:- Event name: The name of the callback prop (e.g.,
onPress,onChange) - Arguments: The data passed to the callback, formatted for easy reading
- Timestamp: When the action was triggered
- Call count: How many times the action has been called
Panel Features
- Clear: Remove all logged actions
- Expand/Collapse: View detailed event data
- Scroll: Review action history
TypeScript Support
Actions work seamlessly with TypeScript and preserve type information:Testing with Actions
Actions are particularly useful when combined with interaction testing:Login.stories.tsx
Differences from Web Actions
The on-device Actions addon works similarly to the web version, with a few differences:- Native rendering: The Actions panel is rendered natively on your device
- Performance: Optimized for mobile devices
- Event objects: May differ from web events (e.g., React Native’s
SyntheticEvent)
For more information about actions in web Storybook, see the official actions documentation. Most concepts apply to React Native, though the implementation is specific to mobile.
Debugging Tips
Actions Not Appearing
If actions aren’t showing up:- Verify the addon is registered in
.rnstorybook/main.ts - Check that you’re using
fn()fromstorybook/test - Ensure the component is actually calling the callback
- Open the Actions panel in the Storybook UI
Clear Action History
To clear logged actions during development, use the clear button in the Actions panel.Related Resources
Controls Addon
Combine with Controls to test different prop combinations
Web Actions Docs
Official web actions documentation