2015-09-25 23 views
13

Tôi có một cấu trúc ứng dụng mà chủ yếu là bắt chước ví dụ Redux real-world nhưng không thể vượt qua nhận được lỗi này:Phản ứng-Redux kết nối phương pháp không thể xác định vị trí lưu trữ trong đạo cụ

Uncaught Error: Invariant Violation: Could not find "store" in either the context or props of "Connect(Home)". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(Home)".

Ở đây sau cấu trúc ứng dụng của tôi :

làm

import { render, createFactory } from 'react' 
import { Provider } from 'react-redux'; 
import router from './router/router' 
import store from './flux/store' 

window.onload =() => { 
    render(createFactory(Provider)({store: store}, 
     () => router 
    ), document.body) 
}; 

Router

import { _ } from 'factories' 
import { Router, IndexRoute, Route } from 'react-router' 
import history from 'history/lib/createBrowserHistory' 

import Home from '../components/home/home' 

let route = _(Route), 
    index = _(IndexRoute); 

let router = _(Router)({history: history()}, 
    route({path: 'home', component: Home}) 
); 

export default router; 

Home Component

import React, { PropTypes } from 'react' 
import { div } from 'factories' 
import { connect } from 'react-redux' 
import { bindActionCreators } from 'redux'; 
import * as users from '../../flux/actions/users' 

class Home extends React.Component { 

    constructor() { 

     super(); 

     this.state = {} 
    } 

    componentWillMount() { 
     console.log(this.props.users.user) 
    } 

    componentWillUnmount() { 

    } 

    render() { 

     return (
      div({}, "Home") 
     ) 
    } 
} 

export default connect(
    state => { 
     return { 
      users: state.users 
     } 
    }, 
    dispatch => bindActionCreators(users, dispatch) 
)(Home) 

Điều này dẫn đến việc các lỗi nêu trên. như bạn có thể thấy tôi đang đi qua cửa hàng theo cách tương tự như trong ví dụ redux.

Trả lời

5

Giải pháp được tìm thấy trong redux troubleshooting docs. Tôi cần phải có bộ phản ứng-bộ định tuyến trả về một chức năng đã tạo bộ định tuyến thực tế:

import { _ } from 'factories' 
import { Router, IndexRoute, Route } from 'react-router' 
import history from 'history/lib/createBrowserHistory' 

import Home from '../components/home/home' 

let route = _(Route), 
    index = _(IndexRoute); 

const router =() => 
    _(Router)({history: history()}, 
     route({path: 'home', component: Home}) 
    ); 
export default router; 
+1

Tác dụng phụ tương tự. Tôi đã có đối tượng createStore() trả về một đối tượng (được bọc trong các dấu ngoặc nhọn). Một khi tôi thay đổi chúng thành niềng răng (tức là trả về một chức năng), lỗi đã biến mất .... nhờ gợi ý. –

Các vấn đề liên quan