redux

How does a redux connected component know when to re-render?

不问归期 提交于 2019-12-17 17:25:03
问题 I'm probably missing something very obvious and would like to clear myself. Here's my understanding. In a naive react component, we have states & props . Updating state with setState re-renders the entire component. props are mostly read only and updating them doesn't make sense. In a react component that subscribes to a redux store, via something like store.subscribe(render) , it obviously re-renders for every time store is updated. react-redux has a helper connect() that injects part of the

When should I add Redux to a React app?

人盡茶涼 提交于 2019-12-17 17:21:52
问题 I'm currently learning React and I am trying to figure out how to use it with Redux for building a mobile app. I'm kind of confused on how the two are related/usable together. For example, I completed this tutorial in React https://www.raywenderlich.com/99473/introducing-react-native-building-apps-javascript, but now I want to play around with adding some reducers/actions to that app and I am not sure where those would tie in with what I've already done. 回答1: React is a UI framework that

How to choose the Redux state shape for an app with list/detail views and pagination?

白昼怎懂夜的黑 提交于 2019-12-17 17:21:09
问题 Imagine I have a number of entries(say, users) in my database. I also have two routes, one for list, other for detail(where you can edit the entry). Now I'm struggling with how to approach the data structure. I'm thinking of two approaches and a kinda combination of both. Shared data set I navigate to /list , all of my users are downloaded from api a stored in redux store, under the key users , I also add some sort of users_offset and users_limit to render only part of the of the list I then

store.getState or mapStateToProps in Component

感情迁移 提交于 2019-12-17 16:37:09
问题 I have a question that what is the difference between use getState from store directly or use mapStateToProps. Please look at me example below import React, { Component } from 'react' import store from '../store' import { connect } from 'react-redux'; class Test extends Component { constructor(props) { super(props); } render() { return ( <p> <h1>{this.props.count}</h1> <h2>{store.getState().reducer1.count}</h2> </p> ) } } const mapStateToProps = (state) => ({ count: state.reducer1.count }); /

Calling Dispatch function from a blank javascript file

自作多情 提交于 2019-12-17 16:27:49
问题 So i'm writing a React-Redux web app, and i call dispatch from my react components like this : this.props.dispatch(someAction()); Now i need to call dispatch from a javascript function that is not a React Component, so how do i import the dispatch function and use it in this case ? Thank you. 回答1: The dispatch function is a member of your redux store. If you created and exported your store in a module, it would be as easy as importing the store in your module and calling the dispatch function

Redux: Why is avoiding mutations such a fundamental part of using it?

人走茶凉 提交于 2019-12-17 15:41:02
问题 I'm new to Redux - and I'm really trying to get the big picture of using functional programming to make unidirectional data more elegant. The way I see it- each reducer is taking the old state, creating a new state without mutating the old state and then passing off the new state to the next reducer to do the same. I get that not causing side effects helps us get the benefits of a unidirectional flow of data. I just really don't get what is so important about not mutating the old state. The

Use Connect or pass data as props to children

雨燕双飞 提交于 2019-12-17 12:02:08
问题 I am new to react and redux. I have a scenario where there are nested components like this. A > B > C > D There is a property used in A component and it will be used in D component. So, I have two approaches: Get state from redux store in component A and then pass it along as props to all it's child components even though it will be used only in D component. I should connect to redux store in component D and fetch that property from there. What is the correct approach? 回答1: As Dan Abramov,

Update single value in item array | react redux

北城以北 提交于 2019-12-17 08:53:37
问题 I have a todo list and want to set the state of that item in the array to "complete" if the user clicks on "complete". Here is my action: export function completeTodo(id) { return { type: "COMPLETE_TASK", completed: true, id } } Here is my reducer: case "COMPLETE_TASK": { return {...state, todos: [{ completed: action.completed }] } } The problem I'm having is the new state does no longer have the text associated of that todo item on the selected item and the ID is no longer there. Is this

Deep copy in ES6 using the spread syntax

我的梦境 提交于 2019-12-17 08:53:23
问题 I am trying to create a deep copy map method for my Redux project that will work with objects rather than arrays. I read that in Redux each state should not change anything in the previous states. export const mapCopy = (object, callback) => { return Object.keys(object).reduce(function (output, key) { output[key] = callback.call(this, {...object[key]}); return output; }, {}); } It works: return mapCopy(state, e => { if (e.id === action.id) { e.title = 'new item'; } return e; }) However it

Deep copy in ES6 using the spread syntax

不想你离开。 提交于 2019-12-17 08:52:11
问题 I am trying to create a deep copy map method for my Redux project that will work with objects rather than arrays. I read that in Redux each state should not change anything in the previous states. export const mapCopy = (object, callback) => { return Object.keys(object).reduce(function (output, key) { output[key] = callback.call(this, {...object[key]}); return output; }, {}); } It works: return mapCopy(state, e => { if (e.id === action.id) { e.title = 'new item'; } return e; }) However it