2017 © Pedro Peláez
 

yii2-extension yii2-react

Yii 2 widget for server-side rendering ReactJs

image

b-tokman/yii2-react

Yii 2 widget for server-side rendering ReactJs

  • Monday, September 11, 2017
  • by bTokman
  • Repository
  • 1 Watchers
  • 16 Stars
  • 49 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 1 Open issues
  • 2 Versions
  • 7 % Grown

The README.md

Yii2-React

This is Yii2 widget that able to use ReactJS components in your Yii2 app, with options of server-side rendering., (*1)

Installation

This widget require v8js php extesion. How to setup V8Js PHP extension? Use the links below: - On Linux - On MacOs - On Windows, (*2)

Composer

Set the minimum-stability in your composer.json to dev 2. This widget compile react bundle from npm react and react-dom packages using browserify and uglify-js BUT since composer run scripts only for root composer.json, need to add the following lines to your composer.json: ```composer ... "scripts": { "regenerate_react_bundle": [ "vendor/b-tokman/yii2-react/build-bundles.sh" ], "post-install-cmd":[ "@regenerate_react_bundle", ], "post-update-cmd": [ "@regenerate_react_bundle", ] ..., (*3)

This way composer will run browserify to create react-bundle.js and uglifyjs to minify it, each time after upgrade or install

2. Then run 

```sh
  $ composer require b-tokman/yii2-react

Composer will download yii2-react package with all dependenices, then npm will download react and react-dom npm packages and scripts will compile it., (*4)

Usage

After the installation you'll be able to use the bTokman\react\widgets\ReactRenderer widget in your app., (*5)

ReactRenderer::widget([
    'componentsSourceJs' => <pathToYourComponentJsFile>,
    'component' => <componentName>,
    'props' => [props],
    'options' => [options],
    'useTranspiler' => true //or false
])
  • componentsSourceJs - path to your react components js file
  • component - the name of global variable the contain your React component, you also can use namespased components by dot-notation
  • props - props array that'll be passed to your component
    • options - Array of options that you can pass to widget:
  • prerender - Tells widget to render your component server-side, by default - true
  • tag - The tag of the element where your component would be passed
  • html attributes - HTML attribute that will be added to the wrapper element of your component. Example: 'id' => 'root'.
    • useTranspiler - boolean, whatever to transpile js code, using bable or not. If you dont have JSX or other specific syntax, dont use transpiler, to save some time

All your reactJs components must be in window scope.

Example

In your view file:, (*6)

echo ReactRenderer::widget([
    'componentsSourceJs' => 'js/main.js',
    'component' => 'Main',
    'props' => [ 'title' => 'Hello' ],
]);

Example main.js, (*7)

class Main extends React.Component {
    render() {
        let title = this.props.title;
        return React.createElement(
            "main",
            null,
            React.createElement("div", null, title)
        );
    }
}
window.Main = Main;

Namespased components:, (*8)

echo ReactRenderer::widget([
      'componentsSourceJs' => 'js/layout.js',
      'component' => 'Layout.Header',
      'props' => ['title' => 'Hello'],
]);

Example layout.js, (*9)

class Header extends React.Component {
    render() {
        let title = this.props.title;
        return React.createElement(
            "header",
            null,
            React.createElement("div", null, title)
        );
    }
}
class Layout extends React.Component {
    render() {
        return React.createElement(
            "div",
            null,
            React.createElement(Layout.Header, {title: this.props.title})
        );
    }
}
Layout.Header = Header;
window.Header = Header;

The Versions

11/09 2017

dev-master

9999999-dev

Yii 2 widget for server-side rendering ReactJs

  Sources   Download

MIT

The Requires

 

by Bohdan Tokman

yii2 react reactjs

22/05 2017

0.0.1

0.0.1.0

Yii 2 widget for server-side rendering ReactJs

  Sources   Download

MIT

The Requires

 

by Bohdan Tokman

yii2 react reactjs