redux

深入学习和理解 Redux

最后都变了- 提交于 2020-03-05 09:08:08
本文首发于 vivo互联网技术 微信公众号 链接: https://mp.weixin.qq.com/s/jhgQXKp4srsl9_VYMTZXjQ 作者:曾超 Redux官网上是这样描述Redux,Redux is a predictable state container for JavaScript apps.(Redux是JavaScript状态容器,提供可预测性的状态管理)。 目前Redux GitHub有5w多star,足以说明 Redux 受欢迎的程度。 一、Why Redux 在说为什么用 Redux 之前,让我们先聊聊组件通信有哪些方式。常见的组件通信方式有以下几种: 父子组件:props、state/callback回调来进行通信 单页面应用:路由传值 全局事件比如EventEmitter监听回调传值 react中跨层级组件数据传递Context(上下文) 在小型、不太复杂的应用中,一般用以上几种组件通信方式基本就足够了。 但随着应用逐渐复杂,数据状态过多(比如服务端响应数据、浏览器缓存数据、UI状态值等)以及状态可能会经常发生变化的情况下,使用以上组件通信方式会很复杂、繁琐以及很难定位、调试相关问题。 因此状态管理框架(如 Vuex、MobX、Redux等)就显得十分必要了,而 Redux 就是其中使用最广、生态最完善的。 二、Redux Data flow

How does the behavior of a child component using `react-redux` change when its parent rerenders?

混江龙づ霸主 提交于 2020-03-05 06:07:31
问题 At react-redux's hooks documentation we are warned that useSelector "does not prevent the component from re-rendering due to its parent re-rendering, even if the component's props did not change," unlike connect This is news to me. Does connect prevent re-rendering where a normal child component would not re-render? More specifically, I'm asking about the difference in the re-rendering behavior of the below three scenarios when the parent component re-renders while the store and props remain

Redux not updating state correctly — state stays as empty array despite adding an object in the line prior

旧巷老猫 提交于 2020-03-05 06:05:53
问题 I'm trying to update my React Redux state using the usual practice. You immutably copy your current state into the return function and then overwrite the properties which are to have a new value. My expected result is to have state.copy update to [ { webCopy: "", id: 0 }, { webCopy: "", id: 1 } ] Unfortunately, it's an empty array despite returning the copy value as a new array with a JS object inside. I've tried looking at StackOverflow search results for "react redux state not updating

Redux not updating state correctly — state stays as empty array despite adding an object in the line prior

落爺英雄遲暮 提交于 2020-03-05 06:05:32
问题 I'm trying to update my React Redux state using the usual practice. You immutably copy your current state into the return function and then overwrite the properties which are to have a new value. My expected result is to have state.copy update to [ { webCopy: "", id: 0 }, { webCopy: "", id: 1 } ] Unfortunately, it's an empty array despite returning the copy value as a new array with a JS object inside. I've tried looking at StackOverflow search results for "react redux state not updating

React js and Redux : Objects are not valid as a React child

安稳与你 提交于 2020-03-05 06:04:44
问题 action; export const ON_MESSAGE = 'ON_MESSAGE'; export const sendMessage = (text, sender = 'user') => ({ type: ON_MESSAGE, payload: { text, sender }, }); reducer: import { ON_MESSAGE } from 'Redux/actions/Chat_action'; import { act } from 'react-dom/test-utils'; const initalState = [{ text: [] }]; const messageReducer = (state = initalState, action) => { switch (action.type) { case ON_MESSAGE: return [...state, action.payload]; default: return state; } }; export default messageReducer;

Cannot connect remote-redux-devtools in React Native

こ雲淡風輕ζ 提交于 2020-03-05 04:07:01
问题 I cannot connect remote-redux-devtools debugger in chrome and getting this error: SocketProtocolError { name: "SocketProtocolError", message: "Socket hung up", code: 1006, stack: "SocketProtocolError: Socket hung up at SCSocke…e?platform=android&dev=true&minify=false:2648:44)" } my code is the following: import thunk from 'redux-thunk'; import {persistReducer, persistStore} from 'redux-persist'; import {composeWithDevTools} from 'remote-redux-devtools'; import * as actions from './actions';

Cannot connect remote-redux-devtools in React Native

大兔子大兔子 提交于 2020-03-05 04:05:47
问题 I cannot connect remote-redux-devtools debugger in chrome and getting this error: SocketProtocolError { name: "SocketProtocolError", message: "Socket hung up", code: 1006, stack: "SocketProtocolError: Socket hung up at SCSocke…e?platform=android&dev=true&minify=false:2648:44)" } my code is the following: import thunk from 'redux-thunk'; import {persistReducer, persistStore} from 'redux-persist'; import {composeWithDevTools} from 'remote-redux-devtools'; import * as actions from './actions';

Action-Reducer Mapping has type error with multiple action types

假装没事ソ 提交于 2020-03-05 02:18:54
问题 I asked a question earlier today, based on my attempts to create a mapping between redux-action-types and reducers to handle each of those types, with the requirement that each action type is handled explicitly. I received a great answer on how to do that, and I can now create such a mapping, but I'm getting an error when I try and use it to create a reducer. If I have the following code (sorry, it's rather long, but I believe it is a minimal example): export type IActionReducerMapping<S, A

ReactJs : What is the proper way of accessing the state data in the root component where the link between React and Redux has been implemented?

孤人 提交于 2020-03-04 18:55:10
问题 DISCLAIMER: This error has been mentioned on stackoverflow before but my case is different. I have had a problem with the user login which I have explained in this stackOverFlow question. I invite you to go and quickly take a look. So as I mentioned in that question in my root file I have this: App.js class App extends Component { render() { const authLinks = ( <Switch> <Route exact path="/" name="Login Page" render={props => <Login {...props} />} /> <Route exact path="/404" name="Page 404"

catchError cancel all parallel requests

狂风中的少年 提交于 2020-03-04 17:37:27
问题 I made multiple requests in parallel. then I throw error when timeout. getAllDirections() { ... return from(urls).pipe( // a list of api urls mergeMap((urlParam, index) => this.http.get<ISchedules>(urlParam[0]).pipe( map(dataRes => { return dataRes; }), timeout(6000), catchError(error => { console.log(error); return throwError(`Request timeout ${error}`); }) ) ) ); Then in my effect I capture the error $LoadSchedulingsByDirectionsByBranch = createEffect(() => this.actions$.pipe( ofType<any>