Cerebral is a state management tool that helps you structure your application logic and debug with ease. Watch the introduction video to learn the core concepts:
Add Cerebral to your project:
# Using npm
npm install cerebral
# Using yarn
yarn add cerebral
You also need to install a view integration.
Here’s a minimal example to get started with Cerebral and React:
// index.js
import React from 'react'
import { createRoot } from 'react-dom/client'
import App from 'cerebral'
import { Container } from '@cerebral/react'
import Devtools from 'cerebral/devtools'
// Define your main module with state and sequences
const main = {
state: {
count: 0
},
sequences: {
increment: ({ store }) => store.set('count', store.get('count') + 1),
decrement: ({ store }) => store.set('count', store.get('count') - 1)
}
}
// Initialize the app with optional devtools
const app = App(main, {
devtools:
process.env.NODE_ENV === 'production'
? null
: Devtools({ host: 'localhost:8585' })
})
// Create a simple counter component
function Counter({ get, sequences }) {
const count = get(state`count`)
return (
<div>
<h1>Count: {count}</h1>
<button onClick={sequences.increment}>+</button>
<button onClick={sequences.decrement}>-</button>
</div>
)
}
// Connect the component to Cerebral
const ConnectedCounter = connect(Counter)
// Render the app
const root = createRoot(document.getElementById('root'))
root.render(
<Container app={app}>
<ConnectedCounter />
</Container>
)
The Cerebral Debugger is a standalone application that connects to your app and provides visualization of state, sequences, and component updates.
Download the Cerebral Debugger for your system:
After installation, open the debugger application and create a new debugger instance. The debugger automatically notifies you of updates.