84

在 Redux 中,我可以轻松订阅存储更改

store.subscribe(() => my handler goes here)

但是,如果我的商店充满了不同的对象,并且在我的应用程序的特定位置我想订阅仅在商店中特定对象中所做的更改,该怎么办?

4

4 回答 4

131

直接使用时无法订阅部分store subscribe,但正如Redux的创建者自己所说——不要subscribe直接使用!为了让 Redux 应用程序的数据流真正发挥作用,您需要一个组件来包装整个应用程序。该组件将订阅您的商店。您的其余组件将成为此包装器组件的子组件,并且只会获取它们需要的状态部分。

如果你将 Redux 与 React 一起使用,那么有一个好消息——官方的react-redux包会为你解决这个问题!它提供了包装组件,称为<Provider />. 然后,您将拥有至少一个“智能组件”来侦听Provider从商店传递的状态更改。您可以指定它应该侦听状态的哪些部分,并且这些状态部分将作为道具传递给该组件(当然,它可以将这些传递给它自己的孩子)。您可以通过在“智能”组件上使用connect()mapStateToProps函数并将该函数用作第一个参数来指定它。回顾一下:

Provider用订阅存储更改的组件包装根组件

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
)

<App />现在,它的任何子connect()组件都将是一个“智能”组件。您可以传入mapStateToProps以选择该州的某些部分并将其作为道具。

const mapStateToProps = (state) => {
    return {
        somethingFromStore: state.somethingFromStore
    }
}

class ChildOfApp extends Component {
    render() {
        return <div>{this.props.somethingFromStore}</div>
    }
}

//wrap App in connect and pass in mapStateToProps
export default connect(mapStateToProps)(ChildOfApp)

显然<App />可以有很多孩子,您可以选择mapStateToProps应该为每个孩子听的州的哪些部分。我建议阅读有关React 使用的文档,以更好地理解此流程。

于 2016-03-25T04:32:42.780 回答
25

Redux 只提供了一种通用的方法来了解商店何时更新:subscribe方法。回调subscribe不获取任何可能已更改的信息,因为subscribeAPI 是故意低级的,并且只是在没有参数的情况下运行每个回调。你所知道的是商店已经以某种方式更新。

因此,有人必须编写特定的逻辑来比较旧状态和新状态,看看是否有任何变化。你可以通过使用 React-Redux 来处理这个问题,为你的组件指定一个mapStateToProps函数,componentWillReceiveProps在你的组件中实现,并检查 store 中的特定 props 是否已更改。

还有几个插件库试图处理这种情况:https ://github.com/ashaffer/redux-subscribe和https://github.com/jprichardson/redux-watch。两者基本上都允许您使用不同的方法指定要查看的状态的特定部分。

于 2016-03-25T05:17:50.107 回答
4

创建了一个 hack,以帮助了解订阅者可以根据商店数据进行区分,并具有多种商店功能。

//import { createStore } from 'redux';
let createStore = require('redux').createStore;
let combineReducers = require('redux').combineReducers;
/**
 * This is a reducer, a pure function with (state, action) => state signature.
 * It describes how an action transforms the state into the next state.
 *
 * The shape of the state is up to you: it can be a primitive, an array, an object,
 * or even an Immutable.js data structure. The only important part is that you should
 * not mutate the state object, but return a new object if the state changes.
 *
 * In this example, we use a `switch` statement and strings, but you can use a helper that
 * follows a different convention (such as function maps) if it makes sense for your
 * project.
 */
function counter(state = 0, action) {
    switch (action.type) {
        case 'INCREMENT':
            return state + 1
        case 'DECREMENT':
            return state - 1
        default:
            return state
    }
}

function messanger(state = 'Mr, khazi', action) {
    switch(action.type) {
        case 'WELCOME':
            return 'Hello, Mr Khazi';
        case 'BYE':
            return 'Bye, Mr Khazi';
        case 'INCREMENT':
            return 'Incremented khazi';
        default:
            return state;
    }
};

function latestAction(state = null, action) {
    switch(action.type) {
        case 'WELCOME':
            return '$messanger';
        case 'BYE':
            return '$messanger';
        case 'INCREMENT':
            return '$messanger, $counter';
        case 'DECREMENT':
            return '$counter';
        default:
            return state;
    }
};

let reducers = {
    counts: counter,
    message: messanger,
    action: latestAction
};

let store = createStore(
    combineReducers(reducers, latestAction)
);
  
// Create a Redux store holding the state of your app.
// Its API is { subscribe, dispatch, getState }.
//let store = createStore(counter)

// You can use subscribe() to update the UI in response to state changes.
// Normally you'd use a view binding library (e.g. React Redux) rather than subscribe() directly.
// However it can also be handy to persist the current state in the localStorage.
store.subscribe(() => {
    if(store.getState().action.indexOf('messanger') !== -1) {
        console.log('subscribed for counter actions', store.getState());
    }
});

store.subscribe(() => {
    if (store.getState().action.indexOf('counter') !== -1) {
        console.log('subscribed for messanger actions', store.getState());
    }
});

// The only way to mutate the internal state is to dispatch an action.
// The actions can be serialized, logged or stored and later replayed.
console.log('----------------Action with both subscriber-------------');
store.dispatch({ type: 'INCREMENT' });
console.log('---------------Action with counter subscriber-----------');
store.dispatch({ type: 'DECREMENT' });
console.log('---------------Action with messenger subscriber---------');
store.dispatch({ type: 'WELCOME' });

/*
    every reducer will execute on each action.

*/
于 2019-03-04T13:33:39.173 回答
4

除了 Andy Noelker 所说的之外,mapStateToProps不仅将状态的一部分正确地传递到组件树中,它还订阅了直接在这些订阅的状态部分中所做的更改。

确实,每次mapStateToProp更改状态的任何部分时都会调用您绑定到存储的每个函数,但是与上一次调用相比,调用的结果会变浅 - 如果您订阅的顶级键没有更改(参考保持不变)。然后 mapStateToProps 不会调用重新渲染。所以如果你想让这个概念起作用,你必须保持 mapStateToProps 简单,不要合并,改变类型或任何东西,它们应该简单地传递状态的一部分。

如果您想在订阅时从状态中减少数据,例如您在状态中有列表数据,并且您想将其转换为以id为键的对象,或者您想将多个状态加入数据结构中,您应该结合mapStateToProps从库中,通过在选择器中进行所有这些修改createSelectorreselect选择器是纯函数,它减少和缓存作为输入传入的状态块,如果输入没有改变——它们返回与上次调用完全相同的引用——而不执行减少。

于 2017-06-10T12:36:53.077 回答