Yii2 Widget for Server-side Rendering ReactJS

Yii2 Widget for Server-side Rendering ReactJS

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

Yii2-React

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

Installation

This widget require v8js php extesion. How to setup V8Js PHP extension? Use the links below:

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:

... 
"scripts": {
"regenerate_react_bundle": [
   "vendor/b-tokman/yii2-react/build-bundles.sh"
],
"post-install-cmd":[
   "@regenerate_react_bundle",
],
"post-update-cmd": [
   "@regenerate_react_bundle",
]
...

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

  1. Then run
  $ 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.

Usage

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

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:

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

Example main.js

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:

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

Example layout.js

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;

Download Details:

Author: bTokman

Source Code: https://github.com/bTokman/yii2-react

react reactjs javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How native is React Native? | React Native vs Native App Development

Article covers: How native is react native?, React Native vs (Ionic, Cordova), Similarities and difference between React Native and Native App Development.

Increase Performance of React Applications Via Array JavaScript Methods

Increase Performance of React Applications Via Array JavaScript Methods. We will create a simple event management application in the react to add, update, and delete an event.

Routing in React without React-Router

I have been using React JS in my projects for quite some time now and am used to managing routing in my app using the react-router package. I have always been keen on having as little dependencies in my apps as possible, so, I always felt perturbed by the use of this particular package in simpler apps which did not have complex routes.

The Ugly Side of React Hooks

In this post, I will share my own point of view about React Hooks, and as the title of this post implies, I am not a big fan.

ReactJS: The Javascript Developer’s Guide

This article will walk you through the concepts you would need to know to step into the world of widely used ReactJS.