JSFiddle - React, Tailwind, and code Playground

by rich_harris

HTML

<main></main>

<script id='template' type='text/ractive'>
    <p>Clicking the button creates an implicit mapping between the root and the Foo component, but the observer, created before the implicit mapping was created, doesn't fire after initialisation</p>
    <p><strong>expected result</strong> clicking the button makes the text read 'test: true' and become red</p>
    <p><strong>actual result</strong> the text reads 'test: true', the colour doesn't change</p>
    
    <button on-click='set("test", true)'>change</button>
    
    {{#with dummyContext}}
        <Foo/>
    {{/with}}
</script>

CSS

body {
    font-family: 'Helvetica Neue', arial, sans-serif;
    font-weight: 200;
    color: #353535;
}

h1, h2, h3, h4, h5, h6 {
    font-weight: 200;
}

.error {
    color: #d00;
    font-family: monospace;
}

JavaScript

var Foo = Ractive.extend({
    template: '<p>test: {{test}}</p>',
    onrender: function () {
        var self = this;
        
        this.observe( 'test', ( n, o ) => {
            console.log( `test changed from ${o} to ${n}` );
            if ( n ) this.find( 'p' ).style.color = 'red';
        });
    }
});

var ractive = new Ractive({
    el: 'main',
    template: '#template',
    data: {
        // uncomment this line and it works (the mapping is created before
        // the observer)
        // test: null,
        dummyContext: { x: 1 }
    },
    components: { Foo }
});