【问题标题】:React + Redux : Invariant Violation: Could not find "store" in either the context or props of "Connect(Body)"React + Redux:不变违规:在“Connect(Body)”的上下文或道具中找不到“store”
【发布时间】:2016-04-30 19:46:59
【问题描述】:

我正在尝试使用 react + redux + react-redux + react-router 理念将我的组件连接到商店。即使父级使用 store 封装,我也会收到此错误:

[Invariant Violation: Could not find "store" in either the context or props of "Connect(Body)". 

Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(Body)".]
 name: 'Invariant Violation', framesToPop: 1 }

代码(为简洁起见缩短了导入)

组件/Table.jsx

export default class Table extends React.Component {

  constructor(props) {
    super(props);
  }

  render() {
    return (
        {this.props.barcodes.map(barcode =>
          <p key={barcode.value}>
        )}
        </tbody>
      </table>
    );
  }
}

容器/BarcodeListing.jsx

import Table from 'components/Table'
import { connect } from 'react-redux'

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

@connect(mapStateToProps)
class Body extends React.Component {
  render() {
    return (
      <Table barcodes={barcodes}/>
      );
  }
}

export default class extends React.Component {
  render() {

    return (
      <Container>
        <Body />
      </Container>
      );
  }
}

routes.jsx

import BarcodeListing from 'containers/barcodeListing';

const reducer = combineReducers(Object.assign({}, reducers, {
  routing: routeReducer
}));

const finalCreateStore = compose(window.devToolsExtension())(createStore);

  const store = finalCreateStore(reducer, {}); 

  return (
    <Provider store={store}>
      <Router history={BrowserHistory} onUpdate={onUpdate}>
        <Route path='/' component={BarcodeListing} />
      </Router>
    </Provider>
  );
};

package.json

"react": "shripadk/react-ssr-temp-fix",
"react-dom": "^0.14.6",
"react-hot-loader": "^1.2.7",
"react-redux": "^4.0.6",
"react-router": "^1.0.0-beta3",
"react-style": "^0.5.5",
"redux": "^3.0.6",
"redux-logger": "^2.3.2",
"redux-simple-router": "^1.0.2",

【问题讨论】:

  • 你安装的是react-router-redux还是react-router
  • @DmitryYaremenko 是的,我都安装了。
  • 您似乎没有共享所有代码...例如 - &lt;/tbody&gt; 没有打开标签... 为什么您将&lt;p&gt; 放在tbody 中?跨度>
  • @DmitryYaremenko 是的,我的实际代码非常庞大,所以我只放了可能导致问题的重要部分。
  • 提供模块版本 - react, redux, *-router(s)

标签: javascript reactjs react-router redux react-router-redux


【解决方案1】:

看来你的 React 是0.13.3 版本,有an issue 对应react &lt;0.14。 请参阅 Dan Abramov(redux 作者)的回答:

在 React 0.14 发布之前,Router 1.0 不会以这种方式工作。

顶级路由处理程序 (App) 不是低级处理程序 (DashboardApp) 的所有者,因此上下文不会传播。这将在 React 0.14 中修复。

这意味着您的store 不会从Provider 向下传递到Body...

如果您无法更改 React 的版本,请尝试将 store 作为 prop 传递给 Body 组件。

类似:

import { store } from '../routes.jsx';
...
return (
  <Container>
    <Body store={ store } />
  </Container>
);

【讨论】:

  • 感谢您发现该问题,但在我升级到响应 0.14.6 后,同样的问题仍然存在
  • 您是否尝试将store 属性设置为Body 组件?
猜你喜欢
  • 2018-10-08
  • 1970-01-01
  • 2016-07-12
  • 2017-11-11
  • 1970-01-01
  • 2019-01-19
  • 1970-01-01
  • 2023-03-24
  • 2018-03-14
相关资源
最近更新 更多