【问题标题】:React not rendering component with URL param使用 URL 参数反应不渲染组件
【发布时间】:2017-06-13 14:16:29
【问题描述】:

我正在创建一个包含以下内容的网站:

  1. 包含项目列表的页面。
  2. 将显示有关单个项目的更多详细信息的页面

我在站点中使用react-router ^3.0.0react-router-redux ^4.0.7webpack 2.1.0-beta.20。我正在尝试像这样设置我的路线:

import 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import thunk from 'redux-thunk';
import {Router, Route, browserHistory} from 'react-router';
import {combineReducers, createStore, applyMiddleware} from 'redux';
import {Provider} from 'react-redux';
import {routerReducer, syncHistoryWithStore} from 'react-router-redux';
import {IntlProvider, intlReducer} from 'react-intl-redux';
import {addLocaleData} from 'react-intl';
import en from 'react-intl/locale-data/en';

import {Home} from './app/components/Home/Home';
import Profile from './app/components/Profile/Profile';
import Login from './app/components/Login/Login';
import SignUp from './app/components/SignUp/SignUp';
import Items from './app/components/Items/Items';
import Item from './app/components/Item/Item';
import {requireAuthentication} from './app/components/General/AuthenticatedComponent';
import {reducers} from './app/reducers/reducers';
import {i18n} from './app/i18n/i18n';

import './index.scss';

addLocaleData([
    ...en
]);

// Create the store from the reducers
const store = createStore(combineReducers({
    reducers,
    routing: routerReducer,
    intl: intlReducer
}), i18n, applyMiddleware(thunk));

// Create an enhanced history that syncs navigation events with the store
const history = syncHistoryWithStore(browserHistory, store);

ReactDOM.render(
    <Provider store={store}>
        <IntlProvider locale="en" defaultLocale="en">
            <Router history={history}>
                <Route path="/" component={Home}/>
                <Route path="/profile" component={requireAuthentication(Profile, true)}/>
                <Route path="/login" component={requireAuthentication(Login, false)}/>
                <Route path="/signup" component={requireAuthentication(SignUp, false)}/>

                {/* Items route lists all of the items, Item shows details for one individual item */}
                <Route path="/items" component={Items}/>
                <Route path="/items/:itemId" component={Item}/>
            </Router>
        </IntlProvider>
    </Provider>,
    document.getElementById('root')
);

ItemsItem 组件是不相关的,不相互依赖。

当我导航到http://localhost/items 时,Items 组件会呈现。但是当我导航到http://localhost/items/1 时,没有任何渲染,并且我在控制台中收到以下错误:

GET http://localhost:3000/items/index.js

我尝试了几种方法来解决这个问题,每种方法都会导致相同的错误消息

...
<Route path="/items" component={Items}/>
<Route path="/items/:itemId" component={Item}/>
...

...
<Route path="/items" component={Items}>
    <Route path="/:itemId" component={Item}/>
</Route>
...

...
<Route path="/items">
    <IndexRoute component={Items}/>
    <Route path="/:itemId" component={Item}/>
</Route>
...

当我这样做时,我可以让 Item 组件与参数一起呈现

...
// Navigate to http://localhost:3000/1
<Route path="/:itemId" component={Item}/>
...

但这不是我希望设置 URL 的方式。有谁知道如何解决这个问题?感谢您的帮助!

【问题讨论】:

  • 您在Items 组件中设置了{this.props.children} 吗?因为Item 依赖于Item,如果没有设置Item 将永远不会渲染。
  • @KeithA,只有嵌套它们才重要吗? (例如,第 2 次/第 3 次尝试)。我曾尝试将{this.props.children} 放入Items,但出现相同的错误。此外,我不希望 Item 组件在 Items 内部呈现,因为它们具有完全不同的布局。
  • 哦,是的,对不起,我应该提到这一点。这个错误GET http://localhost:3000/items/index.js 是整个错误吗?您介意发布ItemItems 组件吗?
  • 当我嵌套路由时,我得到两个GET http://localhost:3000/items/index.js 错误(当我不嵌套它时我只得到1)。它链接到我的index.html 文件中编译和插入index.js 文件的行(在&lt;body&gt; 标记的末尾)。这就是整个错误,所以我没有什么可做的
  • 嗯,好的。您介意发布ItemItems 组件吗?

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


【解决方案1】:

如果您不想在Items 中渲染Item,请尝试此方法

<Route path="/items">
  <IndexRoute component={ Items } />
  <Route path=":itemId" component={ Item } />
</Route>

react-router中,如果你把/放在路径前面,它总是变成绝对路由(example.org/:itemId),没有路由是相对的(example.org/items/:itemId)。

希望这会有所帮助!

【讨论】:

  • 试过了,还是不行。我仍然遇到同样的错误
  • 1) 您是否也有在本地运行的后端服务器@TFischer,2) 仅当您尝试从浏览器地址栏导航到 url 或单击 @ 时才会出现这些错误987654328@有路径吗?
  • 帮我试试这个。将&lt;Link to="/items/1"&gt;Link&lt;/Link&gt; 来自react-router 添加到您的代码中,然后点击即可查看。
【解决方案2】:

所以事实证明这不是react-router 问题,而是webpack 问题。我看了一下正在编译的源代码,看起来是这样的

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width">

        <link href="https://fonts.googleapis.com/css?family=Lato:300,400,7s00" rel="stylesheet">
        <link rel="icon" type="image/png" href="http://fountainjs.io/assets/imgs/fountain.png" />
        <title>My Web App</title>

        <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.2/semantic.min.css></link>
    </head>
    <body>
        <!-- Entry point for the application -->
        <div id="root"></div>

        <script type="text/javascript" src="index.js"></script>
    </body>
</html>

index.js 文件应该链接为/index.js,否则它会在路由所在的当前目录中查找。所以在我的webpack.conf.js 文件中,我找到了提到index.js 的部分

output: {
    path: path.join(process.cwd(), conf.paths.tmp),
    filename: 'index.js'
}

我只是把它改成了

output: {
    path: path.join(process.cwd(), conf.paths.tmp),
    filename: '/index.js'
}

它奏效了。 index.js 文件现在已在源中正确链接

【讨论】:

    猜你喜欢
    • 2021-06-04
    • 1970-01-01
    • 1970-01-01
    • 2018-08-26
    • 2021-04-24
    • 1970-01-01
    • 2017-12-13
    • 2020-03-08
    • 2020-01-08
    相关资源
    最近更新 更多