简体   繁体   English

React-Redux:连接期间状态为空

[英]React-Redux: State is empty during connect

There are similar questions, but the solutions are not working for me.有类似的问题,但解决方案对我不起作用。 I cannot see where we instruct React-Redux to put some value on the state.我看不到我们在哪里指示 React-Redux 在状态上放置一些值。 In the following component this part fails (state) => ({ ingredients: state.ingredients }).在以下组件中,这部分失败 (state) => ({成分:state.ingredients })。 The reason: state is undefined原因:状态未定义

import React from 'react';
import { connect } from 'react-redux'

class Ingredients extends React.Component {

    render() {
        return (
            <div>Ingredients</div>
        );
    }
}

export default connect(
    (state) => ({ ingredients: state.ingredients }),  //This fails!
    (dispatch) => ({ 
        createNew: () => dispatch(actions.createNew()), 
        edit: (id) => dispatch(actions.edit(id)) 
    })
)(Ingredients);

The store simply has:这家商店只有:

function AppStore(state, action) {
        return { 
            ingredients: { foo: "bar" } 
        };
    }
}

export default AppStore;

There is no way for ingredients to be empty没有办法让配料为空

The Store is binded to the App as:商店绑定到应用程序为:

var appStore = createStore(AppStore);

ReactDOM.render((
    <BrowserRouter>
        <Provider store={appStore}>
            <Route component={App} />
        </Provider>
    </BrowserRouter>
), document.getElementById('root'));

The App component is just a view which contains the component (ingredients) that I really need to have with redux: App 组件只是一个视图,其中包含我真正需要使用 redux 的组件(成分):

class App extends React.Component {
    render() {
        return (
            <div>
                <Header/>
                <Switch>
                    <Route path='/home' component={Home} />
                    <Route path='/recipes' component={Recipes} />
                    <Route path='/ingredients' component={Ingredients} />
                    <Route render={() => <div>Not found!</div>} />
                </Switch>
                <Footer />
            </div>
    );
  }

The Ingredients component is the one at the start of my post.成分组件是我帖子开头的组件。 I feel like this Redux magic is missing pieces or it is really counter-intuitive.我觉得这个 Redux 魔法缺失了部分,或者它真的违反直觉。

How Ingredients component could know the value of "state"?成分组件如何知道“状态”的值? Where is it injected to the component?它在哪里注入到组件中? How Redux would know on which components to inject the state? Redux 如何知道在哪些组件上注入状态?

I have follow different tutorials and in all of these I can only see createStore is caled, then its assigned to a tag and then "connect" is used to export the component.我遵循了不同的教程,在所有这些教程中,我只能看到 createStore 被调用,然后将其分配给一个标签,然后使用“connect”来导出组件。 Is it normal for me too feel very unconfortable with this kind of setup?我对这种设置也感到非常不舒服是正常的吗? First I feel bad after accepting React as a "component oriented" way of work to end having all the logic in a single store.首先,在接受 React 作为“面向组件”的工作方式以结束将所有逻辑放在一个存储中之后,我感到很糟糕。 Also, I question why the "connect" implementation, when we can have component inheritance (what about extend Redux.ConnectedComponent or something like that?)... Then, all this magical setup... maybe I should try other thing instead of Redux?另外,我质疑为什么“连接”实现,当我们可以拥有组件继承(扩展 Redux.ConnectedComponent 或类似的东西怎么样?)......然后,所有这些神奇的设置......也许我应该尝试其他东西而不是还原?

EDIT: changed the connect part as:编辑:将连接部分更改为:

function mapStateToProps(state) {
    return { ingredients: state.ingredients };
}

function mapDispatchToProps(dispatch) {
    return {
        createNew: () => dispatch(actions.createNew()), 
        edit: (id) => dispatch(actions.edit(id)) 
    };
}

export default connect(mapStateToProps, mapDispatchToProps)(Ingredients);

With this change (just reordered syntax I think), the state is undefined通过此更改(我认为只是重新排序的语法),状态未定义

EDIT 2: My fault, I was importing a different store to the one shown here If I import the correct one, the state is no longer undefined编辑 2:我的错,我正在将不同的商店导入到此处显示的商店如果我导入正确的商店,则状态不再是未定义的

Common practice is to create a separate mapStateToProps function that returns an object, defining a key-value pair for each piece of redux-state you want to make available as a prop in your component.常见的做法是创建一个单独的mapStateToProps函数,该函数返回一个对象,为您希望作为组件中的prop提供的每个 redux-state 部分定义一个键值对。

This will simplify your connect logic significantly.这将显着简化您的connect逻辑。

import React from 'react';
import { connect } from 'react-redux'

class Ingredients extends React.Component {

    render() {
        return (
            <div>{this.props.ingredients.foo}</div>
        );
    }
}

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

export default connect(mapStateToProps, { createNew, edit })(Ingredients)

The connect function will hook the store's state object to the function(s) that was passed as the first argument. connect 函数会将 store 的state对象挂钩到作为第一个参数传递的函数。 Similarly you could do the same with your dispatcher functions, using ES6 to pass them in as an object.同样,您可以对调度程序函数执行相同操作,使用 ES6 将它们作为对象传入。 The dispatch method then gets passed to each of those functions in the 2nd argument.然后将dispatch方法传递给第二个参数中的每个函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM