GithubHelp home page GithubHelp logo

abalmos / cerebral-react Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cerebral-legacy/cerebral-view-react

0.0 2.0 0.0 27 KB

React View layer package for Cerebral

HTML 3.59% JavaScript 96.41%

cerebral-react's Introduction

cerebral-react

React View layer package for Cerebral

The Cerebral Webpage is now launched

You can access the webpage at http://christianalfoni.com/cerebral/

Debugger

You can download the Chrome debugger here.

Install

npm install cerebral-react

API

All examples are shown with ES6 syntax.

Render the app

// Your controller instance
import controller from './controller.js';
import React from 'react';
import {Container} from 'cerebral-react';

// Your main application component
import App from './components/App.js';

// With React 0.14
React.render(
  <Container controller={controller}>
    <App/>
  </Container>
, document.querySelector('#app'));

// Earlier versions
React.render(<Container controller={controller} app={App}/>, document.querySelector('#app'));

Get state in components

Decorator

import React from 'react';
import {Decorator as Cerebral} from 'cerebral-react';

@Cerebral({
  isLoading: ['isLoading'],
  user: ['user'],
  error: ['error']  
})
class App extends React.Component {
  componentDidMount() {
    this.props.signals.appMounted();
  }
  render() {
    return (
      <div>
        {this.props.isLoading ? 'Loading...' : 'hello ' + this.props.user.name}
        {this.props.error ? this.props.error : null}
      </div>
    );
  }
}

You can also use a function on your decorator:

@Cerebral((props) => {
  return {
    item: ['items', props.itemRef]
  };
})

Higher Order Component

import React from 'react';
import {HOC} from 'cerebral-react';

class App extends React.Component {
  componentDidMount() {
    this.props.signals.appMounted();
  }
  render() {
    return (
      <div>
        {this.props.isLoading ? 'Loading...' : 'hello ' + this.props.user.name}
        {this.props.error ? this.props.error : null}
      </div>
    );
  }
}

App = HOC(App, {
  isLoading: ['isLoading'],
  user: ['user'],
  error: ['error']  
});

You can also use a function on your HOC:

App = HOC(App, (props) => {
  return {
    item: ['items', props.itemRef]
  };
});

Mixin

import React from 'react';
import {Mixin} from 'cerebral-react';

const App = React.createClass({
  mixins: [Mixin],
  getStatePaths() {
    return {
      isLoading: ['isLoading'],
      user: ['user'],
      error: ['error']  
    };
  },
  componentDidMount() {
    this.props.signals.appMounted();
  },
  render() {
    return (
      <div>
        {this.state.isLoading ? 'Loading...' : 'hello ' + this.state.user.name}
        {this.state.error ? this.state.error : null}
      </div>
    );
  }
});

Component

import {Component} from 'cerebral-react';

// Stateless
const MyStatelessComponent = Component({
  foo: ['foo']
}, (props) => (
  <h1>{props.foo}</h1>;
));

// Stateful
const MyStatefulComponent = Component({
  foo: ['foo']
}, {
  getInitialState() {
    return {
      bar: 'bar'
    }
  },
  render() {
    return <h1>{this.props.foo + this.state.bar}</h1>;
  }
});

// No Cerebral state. Same for stateful component
const MyStatelessComponent = Component((props) => (
  <h1>Hello world</h1>
));

You can also use a function to define state paths:

const MyStatelessComponent = Component((props) => (
  {
    foo: ['foo', props.bar]
  }
), (props) => (
  <h1>{props.foo}</h1>;
));

cerebral-react's People

Contributors

christianalfoni avatar codonovan avatar garth avatar guria avatar idream3 avatar

Watchers

 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.