This is Yii2 widget that able to use ReactJS components in your Yii2 app, with options of server-side rendering.
This is Yii2 widget that able to use ReactJS components in your Yii2 app, with options of server-side rendering.
This widget require v8js php extesion. How to setup V8Js PHP extension? Use the links below:
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
$ 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.
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'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
window
scope.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;
Author: bTokman
Source Code: https://github.com/bTokman/yii2-react
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. We will create a simple event management application in the react to add, update, and delete an event.
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.
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.
This article will walk you through the concepts you would need to know to step into the world of widely used ReactJS.