2018-04-03 14:19:05 +02:00
# Declarative State & Side-effect management with [CerebralJS](https://cerebraljs.com/)
## How to use
### Using `create-next-app`
Execute [`create-next-app` ](https://github.com/segmentio/create-next-app ) with [Yarn ](https://yarnpkg.com/lang/en/docs/cli/create/ ) or [npx ](https://github.com/zkat/npx#readme ) to bootstrap the example:
```bash
npx create-next-app --example with-cerebral with-cerebral-app
# or
yarn create next-app --example with-cerebral with-cerebral-app
```
### Download manually
2018-07-11 23:56:15 +02:00
Download the example:
2018-04-03 14:19:05 +02:00
```bash
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-cerebral
cd with-cerebral
```
Install it and run:
```bash
npm install
npm run dev
# or
yarn
yarn dev
```
Deploy it to the cloud with [now ](https://zeit.co/now ) ([download](https://zeit.co/download))
```bash
now
```
## The idea behind the example
Use [CerebralJS ](https://cerebraljs.com/ ) to manage an apps state and side effects in a declarative manner:
Declarative CerebralJS:
```js
2019-05-29 13:57:26 +02:00
;[
2018-04-03 14:19:05 +02:00
setLoading(true),
getUser,
{
success: setUser,
2019-05-29 13:57:26 +02:00
error: setError,
2018-04-03 14:19:05 +02:00
},
2019-05-29 13:57:26 +02:00
setLoading(false),
]
2018-04-03 14:19:05 +02:00
```
vs imperative JS:
```js
function getUser() {
2019-05-29 13:57:26 +02:00
this.isLoading = true
2018-04-03 14:19:05 +02:00
ajax
2019-05-29 13:57:26 +02:00
.get('/user')
2018-04-03 14:19:05 +02:00
.then(user => {
2019-05-29 13:57:26 +02:00
this.data = user
this.isLoading = false
2018-04-03 14:19:05 +02:00
})
.catch(error => {
2019-05-29 13:57:26 +02:00
this.error = error
this.isLoading = false
})
2018-04-03 14:19:05 +02:00
}
```