The idea of this repository is to implement all new concepts and libraries which work great for React.js.
Templates are written in marko.js with predefined template helpers. To see its usage, please refer to layout/application.marko
.
I use webpack-isomorphic-tools to support loading assets inthe server side. You can see the configuration file under config
folder.
Takes a look at templates/todos
, I will have sth like:
createRedialEnhancer({
[FETCH_DATA_HOOK]: ({ store }) => store.dispatch(fetchTodos()),
[UPDATE_HEADER_HOOK]: ({ store }) => store.dispatch(updateLink([
// window.javascriptAssets will be injected to do preload link for optimizing route
{ rel: 'preload', href: window.javascriptAssets['static-page'], as: 'script' },
])),
})
The default require
node statement has been modified by webpack-isomorphic-tools, so I remap it with nodeRequire
under global
. For example, you can use it like as below:
const { ROOT, PUBLIC } = global.nodeRequire('./config/path-helper');
Note: nodeRequire
will resolve the path from project root directory.
To be able to support for asynchronous chunks loading using <link rel='preload' ... />
, I returned the javascriptassets map for all the routes to the client via window.javascriptAssets
.
You can use this to inject assets for the next page to improve performance. This is what I am trying to achievepreload-webpack-plugin.
This will map the hook with the current component and trigger it (Note: This will only be applied to root component).
For now, the best way is to place all logic in the same place with components to make it less painful when scaling the application.Current structure is the combination of ideas from organizing-redux andducks-modular-redux. Briefly, I will have our reducer, action-types, and actionsin the same place with featured components.
Some great ideas from scoped-selectors-for-redux-modules.You can create a localized scope for selector using globalizeSelectors
.
export const mountPoint = 'todos';
export const selectors = globalizeSelectors({
getTodos: identity, // it will also work with reselect library
}, mountPoint);
Then in main reducer, you can have sth like this, which helps reduce the coupling with React view
/* @flow */
import { combineReducers } from 'redux';
import todosReducer, { mountPoint as todosMountPoint } from './components/todos/logicBundle';
import routingReducer, { mountPoint as routingMountPoint } from './components/routing/logicBundle';
import helmetReducer, { mountPoint as helmetMountPoint } from './components/helmet/logicBundle';
export default combineReducers({
[todosMountPoint]: todosReducer,
[routingMountPoint]: routingReducer,
[helmetMountPoint]: helmetReducer,
});
Sample for logicBundle:
export const mountPoint = "todos";
export const selectors = globalizeSelectors(
{
getTodos: identity
},
mountPoint
);
export const ADD_TODO = "todos/ADD_TODO";
export const REMOVE_TODO = "todos/REMOVE_TODO";
export const COMPLETE_TODO = "todos/COMPLETE_TODO";
export const SET_TODOS = "todos/SET_TODOS";
export const addTodo: AddTodoActionType = createAction(ADD_TODO);
export const removeTodo: RemoveTodoActionType = createAction(REMOVE_TODO);
export const completeTodo: CompleteTodoActionType = createAction(COMPLETE_TODO);
export const setTodos: SetTodosActionType = createAction(SET_TODOS);
export const fetchTodos = () =>
(dispatch: Function): Promise<TodoType[]> =>
fetch(getUrl("/api/v1/todos"))
.then(res => res.json())
.then((res: TodoType[]) => dispatch(setTodos(res)));
export default handleActions(
{
[ADD_TODO]: (state, { payload: text }) => update(state, {
$push: [{ text, complete: false }]
}),
[REMOVE_TODO]: (state, { payload: index }) => update(state, {
$splice: [[index, 1]]
}),
[COMPLETE_TODO]: (state, { payload: index }) => update(state, {
$splice: [
[index, 1],
[index, 0, { ...state[index], complete: !state[index].complete }]
]
}),
[SET_TODOS]: (state, { payload: todos }) => todos
},
[]
);
$ git clone git@github.com:hung-phan/koa-react-isomorphic.git
$ cd koa-react-isomorphic
$ yarn install
$ yarn run watch
$ yarn run dev
$ SERVER_RENDERING=true yarn run watch
$ yarn run dev
$ yarn run flow-watch
$ yarn run flow-stop # to terminate the server
You need to add annotation to the file to enable flowtype (// @flow
)
$ yarn test
$ yarn run watch
$ yarn run debug
$ yarn run build
$ SECRET_KEY=your_env_key yarn start
$ docker-compose build
$ docker-compose up
Access http://localhost:3000
to see the application
Feel free to open an issue on the repo.
原理: 利用 webpack 打包能在 node 运行的 React 代码,利用 react-dom/server 将 React 代码渲染成 html 字符串返回给客户端 利用 webpack 打包浏览器运行的 React 代码,在客户端用 import { hydrate } from 'react-dom' hydrate 激活(添加事件等) 也可以使用 babel-core/registe
关于 Redux 中间件 Redux 的中间件是定义一个函数,对 dispatch 进行改造,在发出 action 与执行 reducer 之间添加其他功能,这是对 Redux 进行功能拓展的方式。那么这个中间件的实现原理是什么呢?如何写一个 Redux 的中间件呢? 从 react-start 到 co 源码 (一) 这是一个系列文章。主要分为三篇,讲述了 react 开发环境的简单搭建,脚手架
一、Next.js是什么 Next.js是一个基于React的一个服务端渲染简约框架。它使用React语法,可以很好的实现代码的模块化,有利于代码的开发和维护 1.1 Next.js带来了很多好的特性 默认服务端渲染模式,以文件系统为基础的客户端路由 代码自动分隔使页面加载更快 以页面为基础的简洁的客户端路由 以webpack的热替换为基础的开发环境 使用React的JSX和ES6的module,
项目首页:https://lnlfps.github.io/symph-joy 我们希望有一个结合了next.js和dva优点的基础框架,它们都是目前非常流行的前端框架,next.js为我们解决了服务端渲染和零配置的问题,而dva能够更好的管理应用中的业务流程和数据,但两者现有结合的方案里,我们遇到了些问题,并尝试解决这些问题之后,发现直接import集成是无法完整的发挥其各自的优势的,甚至还不得
最近看了下 React SSR相关的东西,这里记录一下相关内容 本文实例代码已经上传到 github,感兴趣的可参见 Basic | SplitChunkV 初识 React SSR nodejs遵循 commonjs规范,文件的导入导出如下: // 导出 module.exports = someModule // 导入 const module = require('./someModule'
koa-react-router koa 2 middleware for React server side rendering and routing with react-router 5. Looking for React Router 3 support see v1 docs.Try React Router 5 though it's awesome! Usage To insta
Koa React Universal koa2、react、react-universal-component、koa-webpack-server、async/await、code-splitting、hot-module-replacement、react-router4、redux-thunk This project is dedicated to build simple yet po
内容 Vue,React,微信小程序,快应用,TS , Koa和JS 一把梭。 star ^_^欢迎star,你的star是我更新的动力^_^ 目录 mini-program-demo:小程序 demomini-program-template:小程序 templatereact-koa:react+koa 的全栈demoreact-mobile:react 的移动端 demodva-umi-te
koa-mobx-react-starter Along with aiming to use the bleeding-edge of JavaScript (within reason- and all thanks to Babel), this repository represents a choice of frameworks and libraries I think work w
Koa-React-Notes-Web This is a simple SPA built using Koa as the backend, Vue as the first frontend, and React as the second frontend. Frontend Vue GitHub Frontend Vue Demo Frontend React GitHub Fronte
Koa art-template view render middleware. support all feature of art-template. Install npm install --save art-template npm install --save koa-art-template Example const Koa = require('koa'); const ren