2017-03-26 48 views
1

我目前正在開發rails/react/redux項目。我最近添加了react-router-redux「^ 4.0.8」和react-router「^ 3.0.2」,現在將當前的LocationBeforeTransitioning添加到每個API請求URL(即應該是:http://localhost:3000/api/posts?term=「test」,但試圖獲取http://localhost:3000/categories/api/posts?term=「test」 。帖子組件React Router Redux,將LocationBeforeTransitions追加到API請求

該項目是相當大的,但我以下一些潛在的相關片段:

routes.js

module.exports = (
    <Route path="/" component={App}> 
    <IndexRedirect to="/categories"/> 
    <Route path="/categories" component={Categories}/> 
    <Route path="/:category" component={Posts}/> 
    <Route path="/categories/posts" component={Posts}/> 
    <Route path="/categories/:post" component={Citations}/> 
    … 
    <Route path="*" component={NotFound}/> 
    </Route> 
) 

configureStore.js

export default function configureStore(initialState) { 
    const composeEnhancers = composeWithDevTools({ 
    }) 
    const router = routerMiddleware(browserHistory); 
    const store = createStore(
    rootReducer, 
    initialState, 
    composeEnhancers(
     applyMiddleware(thunk, router) 
)) 
… 

actions.js

… 
export function fetchPosts (term = '', random = false) { 
    return function(dispatch) { 
    fetch(`${API_URL}posts?term=${term}&random=${random}`, { 
     method: 'get', 
     headers: !sessionStorage.jwt ? default_header : auth_header 
    }) 
    .then(response => response.json()) 
    .then(json => { 
     if (!json || json.error) { 
     let default_error = 'An Error Occured.'; 
     throw `Oops! ${json.exception || default_error}`; 
     } 
     dispatch(requestPostSuccess(term, json)); 
    }) 
    .catch(error => { 
     dispatch(requestPostError(error)); 
    }); 
    } 
} 
… 

Posts.js

… 
class Posts extends Component { 
    componentWillMount(){ 
    if (this.props.location.pathname.slice(1).split('/')[0] == 'categories'){ 
     this.props.actions.fetchPosts('', true) 
    } 
    } 
… 

任何人都知道什麼可能導致這個和/或解決方法嗎?

+0

發生在我身上,解決方案被發現? – lironn

回答

0

您可以在fetchPosts函數中檢查API_URL的值。

相關問題