oo7 Bonds
Now we have our basic dapp harness, we can start introducing more interesting functionality. Without too much ado, let’s get started. Head in to src/client/scripts/app.jsx
. You will see our basic file:
1 | import React from 'react' |
This is a JSX file. Baiscally means that can handle embedded HTML when describing how React components should be rendered.
Your first Bond
The first thing we’ll do is introduce the oo7 library
. This introduces into Javascript the notion of reactive value known as ‘bonds’. Reactive values are similar to normal variables, except that any places in which they are used will automatically upate themselves as the value changes.
The oo7 package is still very much in development, as such there might be one or two rough edges in this tutorial.
Our first example will just demonstrate how Bond
can introduce effortless reactivity by dynamically replicating the contents of an editable text field into a <span>
.
Import the needed components by placing three lines at the top of app.jsx
1 | import { Bond } from 'oo7' |
Next we need to introduce a new instance of a Bond
. It will represent the current contents of a text field. We will initialize it in our class’s contructor. Insert these lines directly into the App
class decalration.
1 | constructor () { |
Your code should now look like this:
1 | import React from 'react' |
Next we need to create the text entry field and the <span>
element(in which the text field’s contents will be reflected). We will use a version of Semantic UI’s Input
element which has been specially modified to propagate the value into a named Bond
. This is called InputBond
.
Similarly, for the span
we will use a special ‘reactive’ version of the span
element which is able to accept Bond
s as children and values of certain properties. This is known as Rspan
.
Change the <div>Hello World</div>
line to:
1 | <div> |
As you see there’s not all that much here, we just tell the text field input InputBond
to place its value into this.bond
and conversely tell Rspan
to display that value from this.bond
.
Run webpack and let it watch your files to ensure your dapp is continuously rebuilt.
1 | npm start |
Transforming Bonds
Bond
s don’t just have to pass on data; they can also represent transformations on the data.
One example of a transform on text would be simple upper-casing. A function to upper-case text would be text => text.toUpperCase()
1 | <Rspan>{this.bond.map(t => t.toUpperCase())}</Rspan> |