React + Redux:不变违规:无法找到"存储"在"连接(身体)"的上下文或道具中

时间:2016-01-24 14:03:40

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

我尝试使用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",

1 个答案:

答案 0 :(得分:3)

您的React似乎是 $('#datetimepicker').datepicker ({ daysOfWeekDisabled: '06', startDate: '+2d' }); 版本,反应0.13.3an issue。 请参阅Dan Abramov(redux作者)回答:

  

它不会以这种方式与路由器1.0一起工作,直到React 0.14出来。

     

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

这意味着您的<0.14无法从store传递到Provider ...

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

类似的东西:

Body