Tutorial

A quick introduction to Melody, looking at some code, discussing some basics.

Setting up a demo project

To make it quick and easy to get up and running with Melody, we've prepared a tool to create a new project.

$ npx create-melody-app myapp

npx is a tool that comes bundled with npm since version 5.2.0. It offers a very fast way to temporarily install a command line tool through npm and run it directly.

create-melody-app will respond with a message along these lines:

Create Melody App

Creating a new Melody app named test-app 

Downloading template. This might take a couple of minutes...

✨  Success! Created myapp in /Users/pago/Development/myapp

Inside that directory, you can run these commands:
  yarn
    Installs the project dependencies.
  yarn start
    Starts the development server.
  yarn build
    Bundles the app into static files in the /public folder.

To start, run:
  cd myapp
  yarn
  yarn start

Follow the instructions and switch to the myapp directory.

Looking at a first component

When you open up src/counter/index.js you'll find a basic component, written using the melody-component and melody-hoc API.

One thing you'll likely notice immediately with Melody is that it splits the UI from the state related logic.

Using melody-streams instead

When starting a new project, we advise to consider the melody-streams API instead of melody-component, melody-hoc and melody-redux. melody-streams offers a more direct state handling with less boilerplate. We've learned over time that the Redux approach employed by melody-component delivers very beautiful code when we invest sufficient effort. However, if insufficient care is taken, it can easily become complex and hard to read - the example above is a pretty good proof for that assumption.

To install melody-streams, please execute the following command in your terminal:

Now start the application by running

Your application will be started at http://localhost:8080/ and you'll be able to look at the counter component shown above and play a little with it.

Now that we've explored the demo application a little bit - and there's not too much to explore anyway - we can get to work. As mentioned before, the melody-streams API offers a better way to implement less complex components such as our counter component.

To convert our component, we need to adapt the code to look like this:

When using melody-streams, we declare a function which takes an observable of props of the component, which are provided by its parent, and returns an observable state. The latest value of that state stream is then available within the template.

Also note that we've replaced the usage of refs with more plain onclick event handlers. When using melody-streams, that is often sufficient when you want to use createState.

Mounting components

The concept of embedding a component within another component is called mounting in Melody and is done by using a special Twig statement: mount.

The mount statement is overloaded to be useful in a few more cases, but the usage above is likely how most of your usages will look. Later chapters will explore additional usages.

Mounting a top-level component

Now we're only missing a look at how our root component is mounted into the DOM. A "root" or "top-level" component is what we call a Melody component that has no parent component but is injected into the DOM directly.

Last updated

Was this helpful?