[PReact] Integrate Redux with Preact
Redux is one of the most popular state-management libraries and although not specific to React, it is widely used with it. This is why the author of Preact has released a package called preact-redux, which is a simple wrapper around the main react-redux package that enables it to be used in a Preact application without any other changes to your codebase. In this lesson we refactor a stateful component to use Redux + Redux-thunk. https://github.com/developit/preact-redux
Install:
yarn add redux redux-thunk preact-redux
Set up:
import {h, render} from 'preact';
import {Provider} from 'preact-redux';
import thunk from 'redux-thunk';
import {createStore, applyMiddleware, compose} from 'redux';
import App from './components/App';
import reducer from './reducer';
const initialState = {
loading: true,
user: null
};
const composeEnhancers =
typeof window === 'object' &&
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ?
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
// Specify extension’s options like name, actionsBlacklist, actionsCreators, serialize...
}) : compose;
const store = createStore(reducer, initialState, composeEnhancers(applyMiddleware(thunk)));
render(
<div>
<Provider store={store}>
<App />
</Provider>
</div>,
document.querySelector('main'));
Reducer:
export default function (state, action) {
switch (action.type) {
case 'FETCH_USER':
return {
user: null,
loading: true
};
case 'USER_FETCHED':
return {
user: action.payload,
loading: false
};
default:
return state;
}
}
Action creator:
const config = {
url: 'https://api.github.com/users'
};
export function fetchUser(username) {
return function (dispatch) {
dispatch({type: 'FETCH_USER'});
fetch(`${config.url}/${username}`)
.then(resp => resp.json())
.then(user => {
dispatch({type: 'USER_FETCHED', payload: user})
})
.catch(err => console.error(err));
}
}
Component:
import {h, Component} from 'preact';
import User from './User';
import {fetchUser} from '../actions';
import {connect} from 'preact-redux';
export class Profile extends Component {
componentDidMount() {
const username = this.props.match.params.user;
this.props.fetchUser(username);
}
render({loading, userState, user}) {
return (
<div class="app">
{(loading && !userState)
? <p>Fetching {user}'s profile</p>
: <User name={userState.name} image={userState.avatar_url}></User>
}
</div>
);
}
}
const mapStateToProps = (state) => {
return {
userState: state.user,
loading: state.loading
};
};
const mapDispatchToProps = (dispatch) => {
return {
fetchUser: (username) => dispatch(fetchUser(username))
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(Profile);
[PReact] Integrate Redux with Preact的更多相关文章
- [Preact] Integrate react-router with Preact
React-router is the community favourite routing solution - it can handle all of your complex routing ...
- [AngularJS NG-redux] Integrate Redux Devtools
In this lesson, we are going to learn how to integrate Redux Devtools into our Angular application. ...
- Preact(React)核心原理详解
原创: 宝丁 玄说前端 本文作者:字节跳动 - 宝丁 一.Preact 是什么 二.Preact 和 React 的区别有哪些? 三.Preact 是怎么工作的 四.结合实际组件了解整体渲染流程 五. ...
- Taro 3.4 beta 发布: 支持 Preact 为应用开辟更多体积空间
项目体积是困扰小程序开发者的一大问题,如果开发者使用 Taro React 进行开发,更是不得不引入接近 100K 的 React 相关依赖,这让项目体积变得更加捉襟见肘.因此,Taro v3.4 的 ...
- Using Immutable in React + React-Redux
React-Redux Introduction React-Redux is a library for React based on Redux package. And the core ide ...
- React与Preact差异之 -- setState
Preact是React的轻量级实现,是React比较好的替代者之一,有着体积小的优点,当然与React之间一定会存在实现上的差异,本文介绍了在 setState 方面的差异之处. 源码分析 首先来分 ...
- [PReact] Handle Simple Routing with preact-router
Some applications only need a very minimal routing solution. This lesson will cover a practical exam ...
- [PReact] Reduce the Size of a React App in Two Lines with preact-compat
Not every app is greenfield, and it would be a shame if existing React apps could not benefit from t ...
- [PReact] Use Link State to Automatically Handle State Changes
Storing and updating values inside a component’s local state (known as controlled components) is suc ...
随机推荐
- 65.Express---express-session
转自:https://blog.csdn.net/zhangweiwtmdbf/article/details/50723816 第一部分 session概述 1.1 session 是什么? Ses ...
- Vue项目自动转换 px 为 rem,高保真还原设计图
技术栈 vue-cli:使用脚手架工具创建项目. postcss-pxtorem:转换px为rem的插件. 自动设置根节点html的font-size 因为rem单位是相对于根节点的字体大小的,所以通 ...
- spark ml阅读笔记
参考文档:http://www.cnblogs.com/huliangwen/p/7491797.html
- MarkDown study:
#MarkDown study:## 区块元素:### 段落和换行 段落:由一个或多个连续的文本行组成,它的前后要有一个以上的空行(空行的定义是显示上看起来像是空的,便会被视为空行.比方说,若某一行只 ...
- kill&&pkill&&killall---删除执行中的程序
命令功能: 发送指定的信号到相应进程.不指定型号将发送SIGTERM(15)终止指定进程.如果无法终止该程序可用“-KILL” 参数,其发送的信号为SIGKILL(9) ,将强制结束进程 使用ps命令 ...
- 18/9/21模拟赛-Updated
18/9/21模拟赛 期望得分:100:实际得分:0 qwq 拿到题目第一眼,我去,这不是洛谷原题(仓鼠找Sugar)吗 又多看了几眼,嗯,对,除了是有多组数据外,就是原题 然后码码码....自以为 ...
- php中类的持久化如何实现
php中类的持久化如何实现 一.总结 一句话总结:PHP持久化通过serialize() 和 unserialize() 这两个函数来实现的. 1.持久化之后的对象保存到哪里? 将复杂的数组之类 ...
- HttpUtility.UrlEncode,Server.UrlEncode 的区别
引用: 1.HttpUtility.UrlEncode,HttpUtility.UrlDecode是静态方法,而Server.UrlEncode,Server.UrlDecode是实例方法. 2.Se ...
- 微信小程序实现一个简单的表格
wxml <view class="table"> <view class="tr bg-w"> <view class=&quo ...
- 洛谷 P1724 东风谷早苗
P1724 东风谷早苗 题目描述 在幻想乡,东风谷早苗是以高达控闻名的高中生宅巫女.某一天,早苗终于入手了最新款的钢达姆模型.作为最新的钢达姆,当然有了与以往不同的功能了,那就是它能够自动行走,厉害吧 ...