Code Monkey home page Code Monkey logo

fpreact's Introduction

fpreact (Functional Preact)

fpreact provides an alternative api for creating preact components, heavily inspired by elm. The api includes redux style state management and lends itself to functional programming, avoiding the use of this

It has first class TypeScript support (it's written in it!), but also works great with regular JavaScript.

Install

npm i -S fpreact preact

Example

Find more in the examples folder.

TypeScript

import { h, render, component } from 'fpreact';

enum Msg {
    UpdateName,
}

interface Model {
    name: string;
}

const Greet = component<Model, Msg>({
    model: {
        name: 'world',
    },

    update(msg, model) {
        switch (msg.kind) {
            case Msg.UpdateName:
                return { ...model, name: msg.result.get() };
        }

        return model;
    },

    view(model, dispatch) {
        return (
            <div>
                <h1>Hello, {model.name}</h1>
                <label>
                    <span>Name:</span>
                    <input value={model.name} onInput={dispatch(Msg.UpdateName)} />
                </label>
            </div>
        );
    },
});

render(<Greet />, document.body);

JavaScript (ES6 + JSX)

import { h, render, component } from 'fpreact';

const Msg = {
    // You don't have to use a number here.
    // You could just as easily use "UPDATE_NAME" or anything else if you desire,
    // just make sure each item has a unique value
    UpdateName: 0,
};

const Greet = component({
    model: {
        name: 'world',
    },

    update(msg, model) {
        switch (msg.kind) {
            case Msg.UpdateName:
                return { ...model, name: msg.result.get() };
        }

        return model;
    },

    view(model, dispatch) {
        return (
            <div>
                <h1>Hello, {model.name}</h1>
                <label>
                    <span>Name:</span>
                    <input value={model.name} onInput={dispatch(Msg.UpdateName)} />
                </label>
            </div>
        );
    },
});

render(<Greet />, document.body);

JavaScript (ES5)

'use strict';

var fpreact = require('fpreact');

var Msg = {
    UpdateName: 0,
};

var Greet = fpreact.component({
    model: {
        name: 'world',
    },

    update: function(msg, model) {
        switch (msg.kind) {
            case Msg.UpdateName:
                return Object.assign({}, model, { name: msg.result.get() });
        }

        return model;
    },

    view: function(model, dispatch) {
        return fpreact.h(
            'div',
            null,
            fpreact.h('h1', null, 'Hello, ', model.name),
            fpreact.h(
                'label',
                null,
                fpreact.h('span', null, 'Name:'),
                fpreact.h('input', { value: model.name, onInput: dispatch(Msg.UpdateName) }),
            ),
        );
    },
});

fpreact.render(fpreact.h(Greet, null), document.body);

fpreact's People

Contributors

jamesbirtles avatar

Watchers

James Cloos avatar Johnny Reina avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.