react

package module
v0.0.0-...-509d227 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 23, 2015 License: MIT Imports: 13 Imported by: 0

README

react-go

react-go is a Go wrapper around the React and JSX. It enables you to do server-side rendering with the React.

Overview

Currently react.js and JSXTransformer version are 0.13.3.

Dependencies: react-go use go-duktape to evaluate javascript.

Features

  • Support React serverside rendering.
  • Transform JSX to JS.

Example

func main() {
  rc, _ := react.NewReact()
  jsx, _ := react.NewJSX()

  component, _ := jsx.TransformFile("./example.jsx", map[string]interface{}{
    "harmony":     true,
    "strip_types": true,
  })
  rc.Load(component)

  str, _ := rc.RenderComponent("App", map[string]interface{}{
    "data": []interface{}{
      map[string]interface{}{
        "id":   1,
        "name": "first",
      },
      map[string]interface{}{
        "id":   2,
        "name": "second",
      },
    },
  })
  fmt.Println(str)
}
'use strict';
var React = self.React;

var App = React.createClass({
  getInitialState() {
    return {
      message: "loading..."
    };
  },
  componentDidMount() {
    this.setState({ message: "welcome!" });
  },
  render() {
    var list = this.props.data.map(obj => <li key={obj.id}>{obj.name}</li>);
    return (
      <div>
        <p>server-side rendering sample</p>
        <p>{this.state.message}</p>
        <ul>{list}</ul>
      </div>
    );
  }
});

self.App = App;

output (formatted):

<div data-reactid=".1fei395a42" data-react-checksum="-1544136830">
  <p data-reactid=".1fei395a42.0">server-side rendering sample</p>
  <p data-reactid=".1fei395a42.1">loading...</p>
  <ul data-reactid=".1fei395a42.2">
    <li data-reactid=".1fei395a42.2.$1">first</li>
    <li data-reactid=".1fei395a42.2.$2">second</li>
  </ul>
</div>

Getting Started

Install
$ go get -u github.com/bluele/react-go

Author

Jun Kimura

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Asset

func Asset(name string) ([]byte, error)

Asset loads and returns the asset for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetDir

func AssetDir(name string) ([]string, error)

AssetDir returns the file names below a certain directory embedded in the file by go-bindata. For example if you run go-bindata on data/... and data contains the following hierarchy:

data/
  foo.txt
  img/
    a.png
    b.png

then AssetDir("data") would return []string{"foo.txt", "img"} AssetDir("data/img") would return []string{"a.png", "b.png"} AssetDir("foo.txt") and AssetDir("notexist") would return an error AssetDir("") will return []string{"data"}.

func AssetInfo

func AssetInfo(name string) (os.FileInfo, error)

AssetInfo loads and returns the asset info for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetNames

func AssetNames() []string

AssetNames returns the names of the assets.

func RestoreAsset

func RestoreAsset(dir, name string) error

Restore an asset under the given directory

func RestoreAssets

func RestoreAssets(dir, name string) error

Restore assets under the given directory recursively

Types

type JSX

type JSX struct {
	// contains filtered or unexported fields
}

func NewJSX

func NewJSX() (*JSX, error)

func NewJSXWithOption

func NewJSXWithOption(opt *Option) (*JSX, error)

func (*JSX) Transform

func (jx *JSX) Transform(source []byte, opt map[string]interface{}) ([]byte, error)

func (*JSX) TransformFile

func (jx *JSX) TransformFile(path string, opt map[string]interface{}) ([]byte, error)

type Option

type Option struct {
	Source []byte
	// size for javascript vm pool.
	PoolSize int
	// name for variable includes component objects. ex. "self"
	GlobalObjectName string
}

func DefaultJSXOption

func DefaultJSXOption() *Option

func DefaultReactOption

func DefaultReactOption() *Option

Returns a default option for react.

func (*Option) Validate

func (opt *Option) Validate() error

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

func NewPool

func NewPool(opt *Option) (*Pool, error)

func (*Pool) Get

func (pl *Pool) Get() *duktape.Context

func (*Pool) Put

func (pl *Pool) Put(vm *duktape.Context)

type React

type React struct {
	// contains filtered or unexported fields
}

func NewReact

func NewReact() (*React, error)

Create a new react object.

func NewReactWithOption

func NewReactWithOption(opt *Option) (*React, error)

Create a new react object using option. opt: Option for react object.

func (*React) Load

func (rc *React) Load(src []byte) error

Load javascript code. src: javascript source

func (*React) LoadFile

func (rc *React) LoadFile(path string) error

Load javascript file. path: path for javascript source file

func (*React) RenderComponent

func (rc *React) RenderComponent(name string, params interface{}) (string, error)

Render react component. name: component name params: component properties

func (*React) RunScript

func (rc *React) RunScript(src string) (string, error)

Run javascript code and returns its result value. src: javascript source

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL