Debugging
LemonadeJS is engineered to offer developers a streamlined, efficient approach to building user interfaces with its micro JavaScript library. Its reactive nature simplifies the process, yet there are instances when even seasoned developers may encounter components that do not render as expected. This guide outlines the most common issues encountered during the development of LemonadeJS and provides strategic insights for troubleshooting.
Components That Are Not Rendering
A common hurdle when working with LemonadeJS is a component failing to render. Typically, this issue stems from an oversight in the component declaration. LemonadeJS provides two primary methods for declaring a component:
Global declaration
LemonadeJS facilitates the reuse of components across your application through global declaration. This method lets you define a component once and instantiate it in multiple places without redefining it.
function Hello() {
const self = this;
return `<div>{{self.message}}</div>`;
}
// Generate the component and append to the DOM
lemonade.setComponents({ Hello: Hello });
Local Declaration
In LemonadeJS, you can declare a component locally within the context of another component. Local declaration restricts the scope of the component, making it accessible only within the component that declares it.
function Hello() {
const self = this;
return `<div>{{self.hello}}</div>`;
}
function MyComponent() {
const self = this;
let template = `<p><Hello title="Hello world"/></p>`;
return lemonade.element(template, self, { Hello });
}
The Problem
LemonadeJS works differently from other libraries when dealing with variables and the template. It does not require transpile, and the variables won't be available at the renderer scope when the HTML is processed.
The following example throws an error because the variable test is not in the same scope when the template is created and appended to the DOM.
The following example gives you the correct value but won't bring any reactivity to your template.
The Solution
You can use the self on your template to get the best of the reactivity, as below.
Empty custom tags
If you have a non-rendered custom component tag in the DOM, you will need to register the component with one of the following options:
-
lemonade.element(template, self, { missingComponent });
-
lemonade.setComponents({ missingComponent });
Non-reactive arrays
There is a limitation on the observer when dealing with arrays. For example, there are no automatic reactions when adding or removing items from an array in the self. For that reason, you must call self.refresh('nameOfTheProperty');