Javascript 在上下文或道具中找不到存储

Javascript 在上下文或道具中找不到存储,javascript,reactjs,redux,react-redux,Javascript,Reactjs,Redux,React Redux,我正在用react编写代码,我刚刚开始使用redux(因为我需要一个排序容器)。然而,我已经被困在一个地方有一段时间了 我得到这个错误- 不变冲突:在上下文或上下文中找不到“存储” “连接(主页)”的道具。将根组件包装在 ,或将“存储”作为道具显式传递给 “连接(主页)” 我尝试过谷歌搜索,根据react redux的说法,这可以通过以下三种方式进行检查: 1.确保页面上没有重复的React实例。 2.确保您没有忘记在中包装根组件 3.确保您正在运行最新版本的React和React Redux。

我正在用react编写代码,我刚刚开始使用redux(因为我需要一个排序容器)。然而,我已经被困在一个地方有一段时间了
我得到这个错误-

不变冲突:在上下文或上下文中找不到“存储” “连接(主页)”的道具。将根组件包装在 ,或将“存储”作为道具显式传递给 “连接(主页)”

我尝试过谷歌搜索,根据react redux的说法,这可以通过以下三种方式进行检查:
1.确保页面上没有重复的React实例。
2.确保您没有忘记在中包装根组件
3.确保您正在运行最新版本的React和React Redux。

我有下面的代码,它是根(在这里,存储是通过提供者定义的)——

从“React”导入React;
从“react Router”导入{Router,browserHistory};
从'react redux'导入{Provider};
从“redux”导入{createStore,applyMiddleware};
从“redux thunk”导入redux thunk;
从“../routes”导入路由;
从“./减速机/减速机”导入减速机;
从“/actions/actions”导入操作;
导出默认类AppRoutes扩展React.Component{
render(){
const store=createStore(还原器、applyMiddleware(reduxThunk));
返回(
滚动到(0,0)}/>
);
}
}
这个错误只发生在两个组件中的一个上-

// No error when connected only in this component
import React from 'react';
import { connect } from 'react-redux';
import * as actions from './actions/actions';

class Dashboard extends React.Component {

  constructor(props) {
    super(props);
  }

  render() {
    return <h1>Hello, {this.props.isAuthenticated.toString()}</h1>;
  }
}

function mapStateToProps(state) {
  return { 
    content: state.auth.content,
    isAuthenticated: state.auth.authenticated
 };
}

export default connect(mapStateToProps, actions)(Dashboard);
//仅在此组件中连接时无错误
从“React”导入React;
从'react redux'导入{connect};
从“./actions/actions”导入*作为操作;
类Dashboard扩展了React.Component{
建造师(道具){
超级(道具);
}
render(){
返回Hello,{this.props.isAuthenticated.toString()};
}
}
函数MapStateTops(状态){
返回{
内容:state.auth.content,
isAuthenticated:state.auth.authenticated
};
}
导出默认连接(MapStateTops、操作)(仪表板);

//尝试连接此组件时引发错误
从“React”导入React;
从“/LoginPage”导入登录页面;
从“/Dashboard”导入仪表板;
从“./Loading”导入加载;
从'react redux'导入{connect};
从“./actions/actions”导入*作为操作;
类。组件{
建造师(道具){
超级(道具);
这个.setState({
加载:正确
});
}
render(){
var inPage=未定义;
如果(此.props.isAuthenticated){
console.log(“登录”);
inPage=;
}
else if(this.state.loading){
console.log(“加载”);
inPage=;
}
否则{
log('Login');
inPage=;
}
返回(
{inPage}
);
}    
}
函数MapStateTops(状态){
这是我的国家({
加载:错误
});
返回{
内容:state.auth.content,
isAuthenticated:state.auth.authenticated
}
}
导出默认连接(MapStateTrops,actions)(主页);

不确定这是否是您的问题所在。所以我可能离基地太远了

但是,我会检查以确保您正在将子项传递给您的组件。我这样做的方式是在我的应用程序类中:

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

class App extends React.Component{
  render(){
    return(
      <div className="container-fluid">
        {this.props.children}
      </div>
    );
  }
}

App.propTypes={
  children: PropTypes.object.isRequired
};

export default connect()(App);
import React,{PropTypes}来自'React';
从'react redux'导入{connect};
类应用程序扩展了React.Component{
render(){
返回(
{this.props.children}
);
}
}
App.propTypes={
子项:PropTypes.object.isRequired
};
导出默认连接()(应用程序);

这是您在连接中使用的唯一文件吗?其他孩子继承了吗?没有。我在其他几个页面中也使用了connect。但是,我的存储配置在不同的文件中,即我的应用程序入口点中。我在文件中没有看到的另一件事是将React应用程序附加到根html文档的位置。您是否在某个地方有这样一行代码:document.getElementById('app')。这应该在提供程序设置之后立即显示在根渲染中。我认为您不应该在mapStateToProps中执行设置状态,此时您不在react组件上下文中。另外,我认为您的AppRoutes组件是错误的,我通常使用组件/路由声明创建存储区和所有与redux相关的变量。@JeremyD我删除了setState,然后切换到您所说的设置(将所有基于redux的内容移到外部)。但它仍然在发生。我整天都在琢磨如何让它发挥作用。
// Error thrown when I try to connect this component
import React from 'react';
import LoginPage from './LoginPage';
import Dashboard from './Dashboard';
import Loading from './Loading';
import { connect } from 'react-redux';
import * as actions from './actions/actions';

class HomePage extends React.Component {

  constructor(props) {
    super(props);
    this.setState({ 
      loading: true
    });
  }

  render() {
    var inPage = undefined;
    if(this.props.isAuthenticated) {
      console.log('Logged in');
      inPage = <Dashboard user = {HomePage.user}/>;
    }
    else if (this.state.loading){
      console.log('Loading');
      inPage = <Loading />;
    }
    else {
      console.log('Login');
      inPage = <LoginPage />;
    }

    return (
      <div>
        {inPage}
      </div>
    );
  }    
}

function mapStateToProps(state) {
  this.setState({
    loading: false
  });
  return {
    content: state.auth.content,
    isAuthenticated: state.auth.authenticated
  }
}

export default connect(mapStateToProps, actions)(HomePage);
import React, {PropTypes} from 'react';
import { connect } from 'react-redux';

class App extends React.Component{
  render(){
    return(
      <div className="container-fluid">
        {this.props.children}
      </div>
    );
  }
}

App.propTypes={
  children: PropTypes.object.isRequired
};

export default connect()(App);