【问题标题】:component not rendering react-router v4组件不渲染 react-router v4
【发布时间】:2017-11-09 10:38:01
【问题描述】:

所以我正在创建的基本应用程序正在尝试从后端 rails api 请求文章。我在后端正确设置了所有内容,并且 ArticleHome.js 返回了我拥有的 rails 控制器中隐含的所有文章。

def index
    render json: Article.all
end

def show
    render json: @article = Article.find(params[:id])
end

由于某种原因,SingleArticleShow.js 和 NewArticle.js 似乎没有渲染它们的组件。我已将它们放置为 App 组件的子组件。当我单击 ArticleHome.js 文件中的 article.title 链接时,位置道具将其路径名更改为 article/:id 但组件不会像 App.js 中所暗示的那样为 SingleArticleShow.js 呈现。浏览器没有给出错误的反馈。

我已将这些用作参考但没有成功:

https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/guides/blocked-updates.md

https://reacttraining.com/react-router/web/guides/redux-integration

dev/js/index.js

import 'babel-polyfill';
import React from 'react';
import ReactDOM from "react-dom";
import {Provider} from 'react-redux';
import {createStore, applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
import promise from 'redux-promise';
import createLogger from 'redux-logger';
import allReducers from './reducers';
import {withRouter, Route, Switch, BrowserRouter} from 'react-router-dom';
import App from './components/App';
import ArticlesHome from './components/ArticlesHome';
import SingleArticleShow from './components/SingleArticleShow';
import NewArticle from './components/NewArticle';

const logger = createLogger();
const store = createStore(
allReducers,
applyMiddleware(thunk, promise, logger)
);

ReactDOM.render(
<Provider store={store}>
        <BrowserRouter history={history}>
            <div>
                <Route path="/" component={App}/>
            </div>
        </BrowserRouter>
</Provider>,
document.getElementById('root')
);

dev/js/components/App.js

import {withRouter, Route, Switch} from 'react-router-dom';
import connect from 'react-redux';
import ArticlesHome from './ArticlesHome';
import NewArticle from './NewArticle';
import SingleArticleShow from './SingleArticleShow';

// require('../../scss/style.scss');

export default class App extends Component{
render(){
    return(
        <div>
        This is our app
            <Switch>
                <Route exact path="articles/new" component={NewArticle} 
/>
                <Route exact path="articles/:id" component=
{SingleArticleShow}/>
                <Route exact path="/" component={ArticlesHome}/>
            </Switch>
        </div>
    ); 
}
}

dev/js/components/ArticlesHome.js

import React, { Component } from 'react';
import { connect } from 'react-redux';
import {getArticles} from '../actions/index';
import {Link} from 'react-router-dom';

class ArticlesHome extends Component{
componentWillMount(){
    this.props.getArticles();
}

renderArticles(){
    return this.props.articles.map((article) => {
        return (
            <li key={article.id}>
                <Link to={"articles/" + article.id }>
                    <h4>{article.title}</h4>
                </Link>
            </li>
        );
    });
}
render(){
    return(
        <div className="container">

        <div>
        <Link to="articles/new" className="btn btn-warning">
        Create Article
        </Link>
        </div>

        Articles Home Page
        <ul>
            {this.renderArticles()}
        </ul>
        </div>
    );
}
}

function mapStateToProps(state){
return {articles: state.articles.all};
}

export default connect(mapStateToProps, {getArticles})(ArticlesHome);

dev/js/components/SingleArticleShow.js

import React, {Component} from 'react';
import { connect } from 'react-redux';
import { getArticle } from '../actions/index';
import { withRouter } from 'react-router-dom';

class SingleArticleShow extends Component{

componentWillMount(){
    console.log('Is this going to print?');
    this.props.getArticle(this.props.article.id);
}

render(){
    if (!this.props.article){
        return <div> Getting article, please wait. </div>;
    }
    return(
        <div className="container">

            <h3> Title: {this.props.article.title}</h3>

        </div>
    );
}
}

function mapStateToProps({articles}, ownProps){
return {article: articles[ownProps.match.params.id]};
}

export default withRouter(connect(mapStateToProps, {getArticle})
(SingleArticleShow));

这是 package.json

{
"name": "react-redux-template",
"version": "1.0.0",
"main": "index.js",
"scripts": {
   "dev": "webpack",
   "start": "webpack-dev-server"
},
"license": "ISC",
"dependencies": {
    "axios": "^0.13.1",
    "babel-core": "^6.10.4",
    "babel-loader": "^6.2.4",
    "babel-polyfill": "^6.9.1",
    "babel-preset-es2015": "^6.9.0",
    "babel-preset-react": "^6.11.1",
    "babel-preset-stage-2": "^6.24.1",
    "babel-register": "^6.9.0",
    "cross-env": "^1.0.8",
    "css-loader": "^0.23.1",
    "expect": "^1.20.1",
    "node-libs-browser": "^1.0.0",
    "node-sass": "^3.8.0",
    "react": "^0.14.3",
    "react-addons-test-utils": "^15.1.0",
    "react-dom": "^0.14.3",
    "react-redux": "4.3.0",
    "react-router-dom": "^4.0.0",
    "react-router-redux": "^5.0.0-alpha.8",
    "redux": "^3.5.2",
    "redux-form": "^4.1.3",
    "redux-logger": "^2.6.1",
    "redux-promise": "^0.5.3",
    "redux-thunk": "^2.1.0",
    "sass-loader": "^4.0.0",
    "style-loader": "^0.13.1",
    "webpack": "^1.13.1",
    "webpack-dev-middleware": "^1.6.1",
    "webpack-dev-server": "^1.14.1",
    "webpack-hot-middleware": "^2.11.0"
  },
  "devDependencies": {
    "babel-plugin-react-transform": "^2.0.2",
    "babel-preset-stage-0": "^6.5.0",
    "react-transform-hmr": "^1.0.4"
  }
}

import {GET_ARTICLES, GET_ARTICLE, CREATE_ARTICLE} from './types';
import axios from 'axios';

const API_URL = "http://localhost:3000/api/v1";

export function getArticles(){
const request = axios.get(`${API_URL}/articles`);

return{
    type: GET_ARTICLES,
    payload: request
}
}

export function createArticle(props){
const request = axios.post(`${API_URL}/articles`, props);

return{
    type: CREATE_ARTICLE,
    payload: request
};
}

export function getArticle(id){
const request = axios.get(`${API_URL}/articles/${id}`);

return{
    type: GET_ARTICLE,
    payload: request
};
}

如果有人可以提供帮助,我将不胜感激。谢谢。

【问题讨论】:

  • 您是否尝试在所有路线和链接中添加前导斜杠? IE。 /articles/new 而不是 articles/new?

标签: reactjs react-router react-redux


【解决方案1】:

我会试试的

"path="/articles/new"

而不是

path="articles/new"

【讨论】:

    猜你喜欢
    • 2018-02-10
    • 2019-01-15
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-20
    • 1970-01-01
    相关资源
    最近更新 更多