Answer the question
In order to leave comments, you need to log in
How to make friends Yii2 with React?
Help the dinosaur - I've been doing everything with jQuery for 5 years now. Now I set myself something more modern - I started with React. And I already want to go back to the cozy living room.
The same Vue for the training project got up immediately, with one connection. And the brainchild of Facebook does not start in any.
So, a couple of conditions: React is inside vendor/npm-asset. And only there. We connect:
class NpmAsset extends AssetBundle
{
public $sourcePath = '@npm';
public $js = [
'react/cjs/react.production.min.js',
'react-dom/cjs/react-dom.production.min.js',
];
public $jsOptions = [ 'position' => \yii\web\View::POS_HEAD ];
}
Answer the question
In order to leave comments, you need to log in
I figured it out, thank the pasta monster. I'll leave a post for all those who have a lot of questions, but few answers:
What is the essence of today's js (I write for the jkver of dinosaurs like myself, who heard about everything new, did not fully understand):
1. Previously, js was a ton of separate files in global space. Zhkveri and after her 10 plugins of different directions. This is still the case now, only now webpack packs even more files, but written in ES with the participation of require and export. The same require, familiar to server languages, inside one file will import another file in which export is registered. In a node out of the box, this works, but in this case we have a backend not a node and it does not work.
2. What is the convenience? Now we have everything in the same categories in folders, as in all normal languages with namespaces, only without namespaces. What no structure
3. We write modules. Running line = module, counting time = module. All this is laid out in a readable form according to the folder structure within the project. We collect them in webpack. Here is an introductory one for webpack, for example: https://habr.com/post/309306/
4. We assembled it with webpack - inside it, Babel will make the code understood by the browser, and not by the node, connect all the JS modules from the folders and give the finished file. We connect it as before. The same Browserify, the same concat, only smarter, or something.
5. We connect the final file as usual on the page - this completes the assembly. Next, our ES5-6 code has already gone with or without react. Then watchers, less-sass-scss-babel with presets and everything else becomes usable through webpack and goes to folders, from where the server gives ready-made and compiled static files.
In this case, it was not possible to make Yii friends with React in the sense that I wanted to do this without breaking the hierarchy - through vendor/npm-asset, without /node_modules/ at the root, this construction does not start - webpack does not see them through require. But node_modules at the root is ok.
1. Scroll down to the "Related Questions" section
2. Follow the link
3. Read until enlightened.
Try the ReactYii2Essentials library , it allows you to use Yii2 as an API for React and adds familiar fields, forms, and other front-end usefulness to React. Also paired with it is a library for Yii2, which adds a React CRUD generator to gii, which allows you to quickly start. It's still in development, but already quite useful.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question