redux

Access Redux store in JavaScript function

耗尽温柔 提交于 2020-02-20 03:26:52
问题 Is it possible to map redux state to a regular javascript function rather than a React component? If not, is there another way for connecting redux state to a function? (obviously without explicitly passing it in as a param) I have tried connecting to a function, but it breaks (I don't have an exact error message. the server just hits an uncaughtException). Since React components can be pure functions, does connect() only work with a class ComponentName extends Component (and the likes)? The

How to handle errors in fetch() responses with Redux-Saga?

浪尽此生 提交于 2020-02-19 07:43:29
问题 I try to handle Unauthorized error from server using redux-saga. This is my saga: function* logIn(action) { try { const user = yield call(Api.logIn, action); yield put({type: types.LOG_IN_SUCCEEDED, user}); } catch (error) { yield put({type: types.LOG_IN_FAILED, error}); } } I fetch data like this: fetchUser(action) { const {username, password} = action.user; const body = {username, password}; return fetch(LOGIN_URL, { method, headers: { 'Accept': 'application/json', 'Content-Type':

How to handle errors in fetch() responses with Redux-Saga?

假如想象 提交于 2020-02-19 07:41:03
问题 I try to handle Unauthorized error from server using redux-saga. This is my saga: function* logIn(action) { try { const user = yield call(Api.logIn, action); yield put({type: types.LOG_IN_SUCCEEDED, user}); } catch (error) { yield put({type: types.LOG_IN_FAILED, error}); } } I fetch data like this: fetchUser(action) { const {username, password} = action.user; const body = {username, password}; return fetch(LOGIN_URL, { method, headers: { 'Accept': 'application/json', 'Content-Type':

一起学习造轮子(二):从零开始写一个Redux

独自空忆成欢 提交于 2020-02-18 08:29:00
一 redux 思想   首先,每一个webApp有且只有一个state tree,为方便管理和跟踪state的变化,也为了减少混乱,redux只允许通过发送(dispatch)action的方式来改变state。旧state在action的作用下产生新state,这个过程叫做reduce,具体逻辑由用户自定义,称为reducer函数。   另外,redux允许在dispatch action与action到达reducer之间拦截action,做更多的动作,即中间件(middleware),这就像插件机制一样,允许多样的扩展,其中一类重要中间件的功能就是处理异步(与服务端通信)。 二、redux使用   redux用起来是这样的:用户先写好处理state各个子部分的多个reducers,通过redux提供的combineReducers函数合并成一个rootReducer;然后选好需要的中间件;reducer和中间件作为参数,通过createStore函数创建Store对象。Store是redux运用起来的核心对象,通过store可以获取state--store.getState(), dispatch action以及订阅state变化store.subscribe()。 1 const rootReducer = combineReducers(reducers); 2

解密国内BAT等大厂前端技术体系-阿里篇(长文建议收藏)

耗尽温柔 提交于 2020-02-17 09:05:06
解密国内BAT等大厂前端技术体系-阿里篇(长文建议收藏) 进入2019年,大前端技术生态似乎进入到了一个相对稳定的环境,React在2013年发布至今已经6年时间了,Vue 1.0在2015年发布,至今也有4年时间了。 整个业界在前端框架不断迭代中,也寻找到了许多突破方向,例如跨平台中的RN、Flutter,服务端GraphQL、Serverless,前端和客户端的融合越来越紧密,前端在Node和Electron的加持下,也扩展了自己的版图到服务端和桌面。 同时,随着前端开发越来越复杂,整个前端研发也经历了人工化->工具化->工程化->智能化的演变。目前各个大厂在工程化实践不断迭代,出现了许多Low/No Code等前端智能化解决方案,工程化实践也深入到研发的各个环节,不断提升前端研发的标准化能力。而且,随着机器学习的加入,各类UI2Code的解决方案也开始出现,前端研发进入了一个完全不同的时代。 随着端上能力的不断增强,现在在端上做的事情越来越多。首先,数据可视化方向,各类图表、地图、3D等等数据可视化的尝试变得越来越多。其次,伴随着人工智能的加持,在端上的人工智能应用也变的普及,减少了服务端的交互,提高了系统的实时响应能力。最后,随着Webassembly等技术的应用,有可能将前端运行能力再提升一个档次,可以进行更为复杂的端上计算。 为了了解当前前端的发展趋势

Wait for sequence of action with a Redux Observable

百般思念 提交于 2020-02-17 05:00:42
问题 I have a use case where I need to wait for a sequence of actions before I dispatch another using Redux Observables. I've seen some similar questions but I cannot fathom how I can use these approaches for my given use case. In essence I want to do something like so: action$ .ofType(PAGINATION_CLICKED) // This action occurred. .ofType(FETCH_SUCCESS) // Then this action occurred after. .map(() => analyticsAction()); // Dispatch analytics. I would also like to cancel and start that sequence over

Redux学习总结

主宰稳场 提交于 2020-02-16 08:09:57
Redux 是 JavaScript 状态容器,提供可预测化的状态管理。 使用Redux应该遵循一下原则: 整个应用共享的state应该存储在store的状态树中,store是唯一的; state不能直接修改,只能通过action表达修改的意图,调用dispatch()修改state; state的修改规则reducers必须是一个纯函数,不能有副作用。 Redux提供的API createStore createStore方法的作用就是创建一个 Redux store 来以存放应用中所有的 state。 createStore(reducer, [preloadedState], [enhancer]) createStore方法接受三个参数 ,后面两个是可选参数: reducer :参数的类型必须是 function。 preloadedState : 这个参数代表初始化的state(initState),可以是任意类型的参数。 enhancer : 这个参数代表添加的各种中间件,参数的类型必须是function。 中间的preloadedState参数可以直接省略不写,redux已经对这种情况做了处理: /* *如果第二参数preloadedState 是function,且第三个参数enhancer 省略, *把第二个参数赋值给enhancer */ if (typeof

Write unit test case for a react component

。_饼干妹妹 提交于 2020-02-16 07:45:19
问题 I am new to the react redux . I am trying to write a unit test case for a component search functionality which looks like, constructor(props) { super(props); this.staate = { search : '' } } onInputChnage = (e) => { this.setState({ search: e.target.value }); } render() { const { jobs: { content } } = this.props; const { search } = this.state; const filteredList = content.filter(job => (job.jdName && job.jdName.toLowerCase().includes(search.toLowerCase())) || (job.technology && job.technology

Write unit test case for a react component

為{幸葍}努か 提交于 2020-02-16 07:44:47
问题 I am new to the react redux . I am trying to write a unit test case for a component search functionality which looks like, constructor(props) { super(props); this.staate = { search : '' } } onInputChnage = (e) => { this.setState({ search: e.target.value }); } render() { const { jobs: { content } } = this.props; const { search } = this.state; const filteredList = content.filter(job => (job.jdName && job.jdName.toLowerCase().includes(search.toLowerCase())) || (job.technology && job.technology

Switching to babel 7 causes jest to show 'unexpected token'

依然范特西╮ 提交于 2020-02-16 06:29:11
问题 I am struggling with this jest error and have no idea how to fix it. Basically I ran the following commands to switch to new babel 7 : npx babel-upgrade --write --install npm install --save-dev @babel/preset-react npm install babel-polyfill Here is my package.json where all of the configs are specified (no .babelrc , no jest.config.js ): { "name": "seqr", "version": "0.2.0", "homepage": "", "devDependencies": { "@babel/cli": "^7.0.0", "@babel/core": "^7.0.0", "@babel/plugin-proposal-class