简体   繁体   English

React/Redux - 无法将存储值发送到组件的道具(mapStateToProps)

[英]React/Redux - Can't send a store value into props of a component (mapStateToProps)

I'm building a Twitter-like app with React & Redux,我正在使用 React 和 Redux 构建一个类似 Twitter 的应用程序,

I have a rootReducer that contains two reducers (user and newTweet), and I want to send the isConnected state value from user reducer to a component called "Accueil".我有一个包含两个减速器(用户和 newTweet)的 rootReducer,我想将 isConnected state 值从用户减速器发送到一个名为“Accueil”的组件。

Here's the component that needs to get the isConnected value from the store state:这是需要从存储区 state 获取 isConnected 值的组件:

function Accueil({ isConnected }) {
  useEffect(() => {
    console.log("is connected ", isConnected); <---- ALWAYS UNDEFINED
  }, [isConnected]);

  return (
    <div className="Accueil">
      {isConnected ? (
        <div class="container">
          <Nav />
          <div class="row">
            <div class="col m12 l3">
              <AccountNav />
            </div>
            <div class="col m12 l6">
              <Feed />
            </div>
            <div class="col m12 l3">
              <Searchbar />
              <TrendingTopic />
              <Infos />
            </div>
          </div>
        </div>
      ) : (
        <NotConnected />
      )}
    </div>
  );
}

const mapStateToProps = (state) => ({
  counter: state.user.isConnected,
});

export default connect(mapStateToProps)(Accueil);

Here's the rootReducer combining the 2 reducers:这是结合了 2 个减速器的 rootReducer:

import { combineReducers } from "redux";

import user from "./user";
import newTweet from "./newTweet";

const rootReducer = combineReducers({
  user,
  newTweet,
});

export default rootReducer;

Here's the user reducer code:这是用户减速器代码:

import { LOGIN, LOGOUT } from "../constants/types";

const initialState = {
  isConnected: false,
  user: {},
};

export default function user(state = initialState, action) {
  switch (action.type) {
    case LOGIN:
      return { ...state, isConnected: true };
    case LOGOUT:
      return { ...state, isConnected: false, user: {} };
    default:
      return state;
  }
}

Does someone has an idea?有人有想法吗? Thank you谢谢

You are not connecting your component to the redux store and your props should be isConnected not counter :您没有将组件连接到 redux 商店,并且您的道具应该是isConnected而不是counter

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

function Accueil({ isConnected }) {
  useEffect(() => {
    console.log("is connected ", isConnected)
  }, [isConnected]);

  return (
    <div className="Accueil">
      {isConnected ? (
        <div class="container">
          <Nav />
          <div class="row">
            <div class="col m12 l3">
              <AccountNav />
            </div>
            <div class="col m12 l6">
              <Feed />
            </div>
            <div class="col m12 l3">
              <Searchbar />
              <TrendingTopic />
              <Infos />
            </div>
          </div>
        </div>
      ) : (
        <NotConnected />
      )}
    </div>
  );
}


const mapStateToProps = (state) => ({
  isConnected: state.user.isConnected,
});

export default connect(mapStateToProps)(Accueil);

In the Accueil component, the name of the prop is isConnected .Accueil组件中,prop 的名称是isConnected If you want to map a value in the redux state to that prop, you should use the same name in mapStateToProps .如果您想 map 中的值 redux state 到该道具,您应该在mapStateToProps中使用相同的名称。

const mapStateToProps = (state) => ({
  isConnected: state.user.isConnected,
});

export default connect(mapStateToProps)(Accueil);

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

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