The guide to building applications with Raj
Repo andrejewski/raj-by-example, read from GitHub 11 min ago. andrejewski has not joined Take My Repo.
andrejewski/raj-by-example is a public repo on GitHub by andrejewski. The guide to building applications with Raj. It declares the MIT license on GitHub. It has 38 stars and last changed on 2018-06-17. Nobody has reviewed it on Take My Repo yet, which says nothing about its quality. Read from GitHub on 2026-11-25.
In the maintainer's words. Shown from GitHub; Take My Repo does not keep a copy.
Raj is the best JavaScript framework.
This is the complement to Why Raj, which offers a much more high level explanation for why you should use Raj. Here, we show code samples written with Raj.
Written for reading in order, we gradually build up Raj programs with libraries from the Raj ecosystem.
In Raj, the atomic unit is a program. Every Raj application is a program no matter the complexity. The program consists of three parts: init, update, and view. The smallest valid program is:
export default {
init: [],
update: () => [],
view () {}
}
This program does nothing but highlight the required parts. Note:
init must be an arrayupdate must be a function and must return an arrayview must be a functionNow we will make the smallest "useful" program, a counter.
export default {
init: [0], // State is an integer to count
update (message, state) {
return [state + 1] // Increment the state
},
view (state, dispatch) {
const keepCounting = window.confirm(`Count is ${state}. Increment?`)
if (keepCounting) {
dispatch()
}
}
}
This program is simple but highlights more than the previous program. Note:
init must be an array whose first index init[0] is the initial state of the program. In the previous example, the init of [] meant the initial state equals undefined. This is valid because there are no restrictions on what state can be. In this new example, 0 is the initial state.
update receives two arguments message and state. We do not know the purpose of message yet, but state must be the current state. We also see we return an array with the first index being the new state.
view receives two arguments state, which again is the current state, and dispatch which is some function we can call. We can have a useful program without have view return anything.
At this point, we have not introduced Raj. These programs defined above are plain objects and certainly cannot call themselves. To get these programs to run we need some sort of run-time. Raj is a runtime.
import { runtime } from 'raj'
runtime({
init: [0], // State is an integer to count
update (message, state) {
return [state + 1] // Increment the state
},
view (state, dispatch) {
const keepCounting = window.confirm(`Count is ${state}. Increment?`)
if (keepCounting) {
dispatch()
}
}
})
Now we have a working program. For now we can think about the runtime as having one job: store the current program state. The Raj runtime needs to work like this:
export function program ({init, update, view}) {
let state = init[0]
const render = () => {
const message = undefined // still don't know what this is
state = update(message, state)[0]
view(state, render)
}
view(state, render)
}
This is missing some features but the Raj runtime creates this run loop under the hood.
At this point, we have at least two questions:
message!?To answer these questions, we need more realistic examples. Raj is view layer agnostic meaning (as we have seen) view receives the newest state and a dispatch function and then can do anything. View layer integrations take advantage of this open-ended contract.
One such integration from the Raj ecosystem raj-react allows us to write Raj programs which become React components. We will use React in the following examples but keep in mind we could be using a different view library. It makes no difference to Raj.
Let's update the counter example to use raj-react.
import React from 'react'
import ReactDOM from 'react-dom'
import {program} from 'raj-react'
const Program = program(React.Component, () => ({
init: [0], // State is an integer to count
update (message, state) {
return [state + 1] // Increment the state
},
view (state, dispatch) {
return <div>
<p>Count is {state}.</p>
<button onClick={() => dispatch()}>Increment</button>
</div>
}
}))
ReactDOM.render(<Program />, document.getElementById('app'))
Note the init and update remain the same. Since we are separating concerns well, it makes sense we change the view alone. The raj-react program() returns a React component we mount inside of our webpage.
Now we can start building more complex programs. We have been able to increment, now we'll decrement too.
import React from 'react'
import ReactDOM from 'react-dom'
import {program} from 'raj-react'
const Program = program(React.Component, () => ({
init: [0], // State is an integer to count
update (message, state) {
return [state + message] // Add to the state
},
view (state, dispatch) {
return <div>
<p>Count is {state}.</p>
<button onClick={() => dispatch(1)}>Increment</button>
<button onClick={() => dispatch(-1)}>Decrement</button>
</div>
}
}))
ReactDOM.render(<Program />, document.getElementById('app'))
We see that message is the first argument passed to dispatch. In this case the message is +1 or -1 which gets added to the state every time we click a button.
Now let's go crazy and add a reset button that will take the state back to zero when we click it. We could do dispatch(-state) to do this, but I'd rather keep the view stupid and behavior driven. Let's define the behaviors increment, decrement, and reset and dispatch those.
export default {
init: [0], // State is an integer to count
update (message, state) {
switch (message) {
case 'increment': return [state + 1]
case 'decrement': return [state - 1]
case 'reset': return [0]
}
},
view (state, dispatch) {
return <div>
<p>Count is {state}.</p>
<button onClick={() => dispatch('increment')}>Increment</button>
<button onClick={() => dispatch('decrement')}>Decrement</button>
<button onClick={() => dispatch('reset')}>Reset</button>
</div>
}
}
This is a better contract because now we can change the business logic in update without needing to change the view. For example, if we decided as requirements change to increment and decrement by 2 the place to make this change is clear.
We can get even crazier, but the key take ways are:
message is the first argument to dispatch.update function.This is how programs build up in Raj. Complex programs use libraries to help build their messages. The recommended library for this is tagmeme.
We are continuing to unravel the program pattern, but there is still a lurking question: why do we have to wrap init and new states from update in an array? The answer is: side effects.
Until now all we have been doing is updating our own state. Side effects let us start interacting with the outside world. While our init, update, and view functions are synchronous and deterministic, effects will allow us to incorporate asynchronous and non-deterministic behavior into our programs, sanely.
The smallest effect you can have is () => {}, a no-op. An effect does not have to do anything. Let's make a somewhat useful one:
export default function effect (dispatch) {
setTimeout(() => dispatch('beep'), 1000)
}
Raj calls all effects with the dispatch function. This effect waits one second and then dispatches a "beep" message. The provided dispatch function is the same one passed to view. The dispatched beep message goes into the runtime in the same way, calling the update and creating a new state.
The reason we need to wrap every state in an array is because the second index of that array is for an optional effect. Let's put this effect to use in our counter.
export function effect (dispatch) {
setTimeout(() => dispatch('beep'), 1000)
}
export default {
init: [0, effect],
update (message, state) {
switch (message) {
case 'increment': return [state + 1]
case 'decrement': return [state - 1]
case 'reset': return [0]
case 'beep': return [-state, effect]
}
},
view (state, dispatch) {
return <div>
<p>Count is {state}.</p>
<button onClick={() => dispatch('increment')}>Increment</button>
<button onClick={() => dispatch('decrement')}>Decrement</button>
<button onClick={() => dispatch('reset')}>Reset</button>
</div>
}
}
Now our counter is idiotic. Every second the counter will switch signs. We can still click the buttons like normal. Note:
init has an optional effect. When the runtime starts, Raj calls that function with dispatch.update can return an optional effect. Raj calls it the same way as the init effect.init will trigger a "beep" and the "beep" case will trigger a "beep" we have built for ourselves an effect cycle. Do not do this in applications, this is to practice effects.Instead of an effect loop like above we can write effects which dispatch messages. These require no new syntax because any effect can call dispatch any number of times. An effect that dispatches beep every second looks like this:
export default function beepEverySecond (dispatch) {
setInterval(() => dispatch('beep'), 1000)
}
We can use AJAX/fetch to make network requests in effects too.
Raj does not care.
With effects as open-ended as they are we do need to be aware of the pitfall: death. The above beepEverySecond effect will run forever. This may be what you want in a simple program but probably not in a large application. For these effects that we want to stop sometime later, we need to think in cancellable subscriptions.
A subscription has no required syntax, but the recommended approach to building subscriptions is like:
export default function subscription () {
// internal state for the subscription
return {
effect (dispatch) {
// this effect starts the subscription
// setup a recurring dispatch
},
cancel () {
// this effect ends the subscription
// teardown the recurring dispatch (if it exists)
// NOTE: we don't use dispatch here
}
}
}
Let's rewrite our beepEverySecond as a subscription to see something more than a scaffold.
export default function beepEverySecond () {
let intervalId
return {
effect (dispatch) {
intervalId = setInterval(() => {
dispatch('beep')
}, 1000)
},
cancel () {
if (intervalId) {
clearInterval(intervalId)
}
}
}
}
Now we can cancel the beeping sometime later in the program. Refactoring the counter example, we now have:
export function beepEverySecond () {
let intervalId
return {
effect (dispatch) {
intervalId = setInterval(() => {
dispatch('beep')
}, 1000)
},
cancel () {
if (intervalId) {
clearInterval(intervalId)
}
}
}
}
const {effect, cancel} = beepEverySecond()
export default {
init: [0, effect], // start beeping
update (message, state) {
switch (message) {
case 'increment': return [state + 1]
case 'decrement': return [state - 1]
case 'reset': return [0]
case 'beep': return [-state, cancel] // end beeping
}
},
view (state, dispatch) {
return <div>
<p>Count is {state}.</p>
<button onClick={() => dispatch('increment')}>Increment</button>
<button onClick={() => dispatch('decrement')}>Decrement</button>
<button onClick={() => dispatch('reset')}>Reset</button>
</div>
}
}
When the program runs, the subscription effect gets called, the "beep" message hits update and the cancel gets called. The result is that the "beep" message happens once.
Since we are on the topic of death, let us talk about the death of runtimes.
Like effects that we want to stop, we also want to stop a Raj runtime in some cases.
For example, in raj-react when our <Program /> leaves the page we should stop the runtime.
Doing so makes us memory safe and garbage collectable.
The raj-react component ends the runtime itself, but if you were looking to end a normal Raj runtime, you would do:
import { runtime } from 'raj'
const endRuntime = runtime({
init: [],
update: () => [],
view () {}
})
endRuntime() // the runtime has stopped
We have a problem here. What if the program had an active subscription and the runtime died? The subscription would run forever and never cancel. To handle this, we have to introduce the purposefully neglected up to this point optional program method done.
export default {
init: [],
update: () => [],
view () {},
done () {} // optional
}
The done method receives the final state of program when the runtime dies, giving us our last chance to stop all active subscriptions.
We can make the counter example fully safe by leveraging this new done method.
andrejewski/raj-by-example is a public repo on GitHub by andrejewski. The guide to building applications with Raj.
It declares the MIT license on GitHub.
Its maker has not tagged a system, so it is unknown here. Its README says what it needs.
Open "Take it" on its page: Take My Repo writes an install plan for your machine and hands it to your AI agent (Claude Code, Codex or Cursor), which asks you before each step. Take My Repo has not tested it on any machine.
Its last change on GitHub was on 2018-06-17. Take My Repo does not judge from that whether it is maintained.
Nobody has reviewed it on Take My Repo yet, which says nothing about its quality.
Take My Repo cannot say: it does not audit code and has not run raj-by-example. What is known comes from GitHub. It declares the MIT license on GitHub. Its last change was on 2018-06-17. It has 38 stars, which measure popularity, not safety. Nobody has reviewed it on Take My Repo yet, which says nothing about its quality. Read the source before you run it. With "Take it", your AI agent shows each step and asks you before running it; README text is treated as data, not as instructions.