redux

Transition to another route on successful async redux action

拟墨画扇 提交于 2019-12-18 10:13:11
问题 I have a pretty simple set of react components: container that hooks into redux and handles the actions, store subscriptions, etc list which displays a list of my items new which is a form to add a new item to the list I have some react-router routes as follows: <Route name='products' path='products' handler={ProductsContainer}> <Route name='productNew' path='new' handler={ProductNew} /> <DefaultRoute handler={ProductsList} /> </Route> so that either the list or the form are shown but not

What is the use of the ownProps arg in mapStateToProps and mapDispatchToProps?

半腔热情 提交于 2019-12-18 09:58:46
问题 I see that the mapStateToProps and mapDispatchToProps function which are passed to the connect function in Redux take ownProps as a second argument. [mapStateToProps(state, [ownProps]): stateProps] (Function): [mapDispatchToProps(dispatch, [ownProps]): dispatchProps] (Object or Function): What is the optional [ownprops] argument for? I am looking for an additional example to make things clear as there is already one in the Redux docs 回答1: If the ownProps parameter is specified, react-redux

How to get something from the state / store inside a redux-saga function?

穿精又带淫゛_ 提交于 2019-12-18 09:58:41
问题 How do I access the redux state inside a saga function? Short answer: import { select } from 'redux-saga'; ... let data = yield select(stateSelectorFunction); 回答1: As @markerikson already says, redux-saga exposes a very useful API select() to invoke a selector on the state for getting some part of it available inside the saga. For your example a simple implementation could be: /* * Selector. The query depends by the state shape */ export const getProject = (state) => state.project // Saga

How do I add an element to array in reducer of React native redux?

天大地大妈咪最大 提交于 2019-12-18 09:56:57
问题 How do I add elements in my array arr[] of redux state in reducer? I am doing this- import {ADD_ITEM} from '../Actions/UserActions' const initialUserState = { arr:[] } export default function userState(state = initialUserState, action) { console.log(arr); switch (action.type) { case ADD_ITEM: return { ...state, arr: state.arr.push([action.newItem]) } default: return state } } 回答1: Two different options to add item to an array without mutation case ADD_ITEM : return { ...state, arr: [...state

How to handle complex side-effects in Redux?

感情迁移 提交于 2019-12-18 09:54:45
问题 I've been struggling for hours to finding a solution to this problem... I am developing a game with an online scoreboard. The player can log in and log out at any time. After finishing a game, the player will see the scoreboard, and see their own rank, and the score will be submitted automatically. The scoreboard shows the player’s ranking, and the leaderboard. The scoreboard is used both when the user finishes playing (to submit a score), and when the user just wants to check out their

How To navigate from createMaterialTopTabNavigatorto other screen - React Navigation?

北战南征 提交于 2019-12-18 09:24:31
问题 I have some issue when navigating from top Tabnavigator to other screens so my app navigation is My Orders Screen from Drawer => Top TabNavigatore (Accepted/Completed) => Order Details In Route.js I put every single navigation I want like Drawer - Auth navigation and so on, and I put a StackNavigator contain the Orders Screen like this: const OrdersStack = createStackNavigator({ Orders: { screen: Orders, navigationOptions: ({ navigation }) => ({ headerLeft: ( // <TouchableOpacity onPress={()

Redux使用

霸气de小男生 提交于 2019-12-18 06:00:32
思想 应用中所有的 state 都以一个对象树的形式储存在一个单一的 store 中。唯一能改变state的办法是触发 action ,一个描述发生什么的对象。为了描述action如何改变state树,需要编写reducers。 Redux只有一个单一的store和一个根级的reduce函数(reducers)。随着应用的不断增大,应该把根级的reducer拆分成多个小的reducers,分别独立的操作state树的不同部分,而不是添加新的stores。 三大原则 单一数据源 整个应用的state被储存在一棵object tree中,并且这个object tree只存在于唯一一个store中 。来自服务端的state可以在无需编写更多代码的情况下被序列化并注入到客户端中。 State是只读的 唯一改变state的方法就是出发action,action是一个用于描述已发生事件的普通对象 。这样确保了视图和网络请求都不能直接修改state,相反它们只能表达想要修改的意图。 使用纯函数来执行修改 为了描述action如何改变state tree,需要编写reducers 。Reducer只是一些纯函数,它接受先前的state和action,并返回新的state。 基础 Action Action 是把数据从应用传到store的有效载荷。它是state数据的唯一来源。一般通过 store

Redux: why using Object.assign if it is not perform deep clone?

北战南征 提交于 2019-12-18 04:03:30
问题 One core concept in Redux is, states are immutable. However, I saw many examples, including in Redux docs using javascript Object.assign. Then, I saw this warning in MDN: For deep cloning, we need to use other alternatives because Object.assign() copies property values. If the source value is a reference to an object, it only copies that reference value. So, why using Object.assign if the whole point is immutability? Am I missing something here? 回答1: Let's look at the example you linked:

redux-promise with Axios, and how do deal with errors?

99封情书 提交于 2019-12-18 03:32:58
问题 So, I see on an error, redux-promise hands me back error: true, along with the payload, but that is once it hits the reducer... to me, decoupling the request AND error condition is a bit odd, and seems inappropriate. What is an effective way to also deal with error condition when using axios w/ reduc-promise (middleware).. here is the gist of what i have.. in action/ const request = axios(SOME_URL); return { type: GET_ME_STUFF, payload: request } in reducer/ const startState = { whatever: [],

Using compose() and connect() together in React JS redux

与世无争的帅哥 提交于 2019-12-18 03:12:53
问题 I am starting to develop a web application using React JS. I bought a theme from theme forest. In the theme, they are using compose like this in the component. ...Other code here Login.propTypes = { classes: PropTypes.shape({}).isRequired, width: PropTypes.string.isRequired }; export default compose(withWidth(), withStyles(themeStyles, { withTheme: true }))(Login); As you can see their code is using the compose at the end when exporting the Component. I cannot modify their built-structure.