In addition to solving the dependence on the browser environment, server-side rendering also needs to solve two problems:
The front and back ends can share code
Front-end and back-end routing can be processed uniformly
The React ecosystem provides many options. Here we choose Redux and react-router for illustration.
Redux
Redux provides a set of one-way data flow similar to Flux. The entire application only maintains one Store and is functional-oriented. Features make it friendly for server-side rendering support.
2 minutes to learn how Redux works
About Store:
The entire application only A unique Store
The corresponding state tree (State) of the Store is generated by calling a reducer function (root reducer)
on the state tree Each field can be further generated by different reducer functions
Store contains several methods such as dispatch and getState to process the data stream
Store's state tree can only be triggered by dispatch(action) to change
Redux's data flow:
action is a { type , payload } object
reducer function is triggered by store.dispatch(action)
reducer function accepts (state, action) two parameters , return a new state
The reducer function determines action.type and then processes the corresponding action.payload data to
So for the entire application, one Store corresponds to one UI snapshot. Server-side rendering is simplified to initializing the Store on the server side, passing the Store to the root component of the application, and calling renderToString on the root component to render the entire application. Output as HTML containing initialization data.
react-router
react-router matches different routing decisions in a declarative way to display different components on the page, and The routing information is passed to the component through props, so as long as the routing changes, the props will change, triggering the component to re-render. Suppose there is a very simple application with only two pages, a list page /list and a details page /item/:id. Click an item on the list to enter the details page. You can define routes like this
, ./routes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<a href="https://www.php.cn/code/8256.html"target="_blank">import React from 'react';
Starting from here, we use this very simple application to explain the front-end and back-end aspects of implementing server-side rendering. Some details.
ReducerStore is generated by reducer, so reducer actually reflects the state tree structure of Store
The state parameter of rootReducer is the state tree of the entire Store. Each field under the state tree can also have its own reducer, so listReducer and itemReducer are introduced here, as you can see The state parameters of these two reducers are just the corresponding list and item fields on the entire state tree.
list is a simple array containing items, which may be similar to this structure: [{ id: 0, name: 'first item'} , {id: 1, name: 'second item'}], obtained from action.payload of 'FETCH_LIST_SUCCESS'.
Then ./reducers/item.js to process the obtained item data
The above is the detailed content of How to use React server-side rendering efficiently. For more information, please follow other related articles on the PHP Chinese website!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn