Appearance
Debugging with DevTools
Learn how to debug your Nexus State applications using the built-in DevTools.
Enabling DevTools
First, enable DevTools when creating your store:
typescript
import { createEnhancedStore, devTools } from '@nexus-state/devtools';
const store = createEnhancedStore([devTools({
name: 'My App',
trace: true,
maxAge: 50
})], {
enableDevTools: true,
enableTimeTravel: true
});Using the DevTools Extension
Installation
Install the Nexus State DevTools browser extension:
Connecting to DevTools
DevTools should connect automatically when enabled. If not, you can manually connect:
typescript
store.connectDevTools();DevTools Features
1. State Inspector
The State Inspector shows all atoms and their current values:
- Click on atoms to see their values
- Expand/collapse atoms to see nested state
- Search atoms by name
2. Action History
The Action History shows all state changes over time:
- Click on actions to see the state before and after
- Filter actions by name
- Jump to specific actions
3. Time Travel
Use the slider to move between different states:
- Drag the slider to see state at any point in history
- Click on specific actions to jump to that state
- See which actions are undoable/redoable
4. Performance Monitor
Monitor performance metrics:
- Track update frequency
- Monitor memory usage
- Identify slow updates
Debugging Common Issues
Issue: State Not Updating
Symptoms: UI doesn't reflect state changes
Solutions:
- Check if the atom is registered (should appear in DevTools)
- Verify that the store is created with DevTools enabled
- Check for subscription issues in your framework integration
Issue: DevTools Not Connecting
Symptoms: DevTools extension shows no state
Solutions:
- Ensure DevTools plugin is added to the store
- Check browser console for errors
- Verify that DevTools is enabled in store options
- Make sure you're using the correct DevTools extension
Issue: Too Many Snapshots
Symptoms: DevTools slows down with many snapshots
Solutions:
- Limit
maxHistoryin store options - Use manual snapshots for important actions
- Clear history periodically
Advanced Debugging Techniques
1. Custom Action Names
Give your actions descriptive names for better debugging:
typescript
store.set(countAtom, 1); // Action: "set countAtom to 1"
// Better
store.captureSnapshot('User Incremented Count');
store.set(countAtom, 1);2. Trace Mode
Enable trace mode to see exactly where state changes originate:
typescript
const store = createEnhancedStore([devTools({
trace: true
})], {
enableDevTools: true
});3. Action Groups
Group related actions for better organization:
typescript
import { createActionGrouper } from '@nexus-state/devtools';
const group = createActionGrouper({
label: 'User Actions',
match: (action) => action.type?.startsWith('user_')
});
store.addGroup(group);4. Custom Snapshots
Create meaningful snapshots at important points:
typescript
// After important user actions
store.captureSnapshot('Form Submitted');
store.captureSnapshot('User Login');
store.captureSnapshot('Data Fetched');Framework-Specific Debugging
React
Use React DevTools alongside Nexus State DevTools:
- Install React DevTools extension
- Use React Profiler to identify slow components
- Check component props for expected state values
Vue
Use Vue Devtools alongside Nexus State DevTools:
- Install Vue Devtools extension
- Check component state in the Vue Devtools inspector
- Use the performance monitor to identify bottlenecks
Svelte
Use Svelte Devtools alongside Nexus State DevTools:
- Install Svelte Devtools extension
- Check component store bindings
- Monitor re-render behavior
Best Practices
1. Use Descriptive Names
Give your atoms and actions descriptive names for easier debugging:
typescript
// Good
const userAtom = atom({ name: '' }, 'user-profile');
store.captureSnapshot('User Updated Profile');2. Monitor Performance
Regularly check the performance monitor in DevTools to identify issues early.
3. Test Time Travel
Use Time Travel to verify your application's state management:
- Perform several actions
- Use undo/redo to verify state changes
- Check that all state transitions are correct
4. Keep History Manageable
Limit history size and use manual snapshots for important actions:
typescript
const store = createEnhancedStore([], {
enableTimeTravel: true,
maxHistory: 50,
autoCapture: false
});Troubleshooting
DevTools Not Showing Any State
- Check if DevTools plugin is properly configured
- Verify that DevTools is enabled in store options
- Check browser console for errors
- Ensure atoms are being created with the
atom()function
DevTools Causes Performance Issues
- Reduce
maxHistorysize - Disable trace mode
- Use production builds for performance testing
- Limit the number of atoms in your store
Time Travel Not Working
- Ensure
enableTimeTravelis set totrue - Check that the store is created with DevTools plugin
- Verify that atoms are registered properly
- Check for errors in browser console