2017-06-21 51 views
3

我有一個主屏幕,博客主列表,然後是博客詳細信息頁面的簡單應用程序。從主屏幕導航到博客列表並返回按預期工作。當我點擊其中一篇博文時,應用程序導航到頁面並呈現博客帖子。但是當我回到博客主列表時,博客帖子現在位於博客列表的最底部。如果我接着回到主頁,則博客文章位於主頁圖形背後的DOM中。爲什麼導航到其他頁面後,我的React組件仍保留在DOM中?

我正在使用react路由器4.1。

是否與我的路線設置

import React, { Component } from 'react' 
import { BrowserRouter as Router, Route } from 'react-router-dom' 
import NavBar from './components/NavBar' 
import Blog from './pages/blog' 
import Home from './pages/home' 
import Post from './pages/post' 
import './css/App.css' 

class App extends Component { 
    render() { 
    return (
     <Router> 
      <div className="App"> 
      <NavBar /> 
      <Route exact path='/' component={Home} /> 
      <Route path='/blog' component={Blog} /> 
      <Route path='/post/:slug' component={Post} /> 
      </div> 
     </Router> 
    ) 
    } 
} 

export default App 

或與我後組件

import React from 'react' 
import fetch from 'isomorphic-unfetch' 
import NavBar from '../components/NavBar' 
import dateUtils from '../utilities/DateAndTime' 

const classNames = require('classnames') 


class Post extends React.Component { 

    constructor(props) { 
     super(props) 

     this.state = { 
      blogObject: null, 
      fetchBlogObject: false 
     } 

     this.fetchBlogObject = this.fetchBlogObject.bind(this) 
     this.createMarkup = this.createMarkup.bind(this) 
     this.assignStyleToCoverImage = this.assignStyleToCoverImage.bind(this) 
     this.formatDateString = this.formatDateString.bind(this) 

    } 

    componentWillMount() { 
     this.fetchBlogObject() 
    } 


    componentWillReceiveProps() { 
     this.fetchBlogObject() 
    } 

    fetchBlogObject() { 

      console.log('fetching') 

      this.setState({ fetchBlogObject: true }) 

      fetch(`*******************`) 
      .then(response => response.json()) 
      .then((responseJson) => { 
       console.log(responseJson) 
       this.setState({ blogObject: responseJson.object }) 
      }) 


    } 


    createMarkup(stringToConvertToHtml) { 
     return { __html: stringToConvertToHtml } 
    } 


    assignStyleToCoverImage() { 

     const style = { 
      background: `url(${this.state.blogObject.metadata.hero.url})`, 
      height: '35vh', 
      backgroundSize: 'cover', 
      backgroundPosition: 'center' 
     } 

     return style 

    } 


    formatDateString(dateString) { 
     console.log(dateString) 

     const d = `${dateUtils.returnMonthFromISODateString(dateString)} ${dateUtils.returnDateNumberFromISOString(dateString)}, ${dateUtils.returnFullYearFromISOString(dateString)} by Phil Andrews` 
     return d 
    } 

    render() { 


     const postContainerClasses = classNames({ 
      'post-container': true 
     }) 

     const postBodyClasses = classNames({ 
      'post-body': true 
     }) 

     return (

      <div> 
       <NavBar /> 

       {this.state.blogObject ? 

        <div className={postContainerClasses} > 
         <div className='header-image' style={this.assignStyleToCoverImage(this.state.blogObject)} /> 
         <div className='post-body-padding'> 
          <div className='post-header-info'> 
           <h1 className='post-title'>{this.state.blogObject.title}</h1> 
           <h4 className='post-date'>{this.formatDateString(this.state.blogObject.created_at)}</h4> 
          </div> 
          <div className={postBodyClasses} dangerouslySetInnerHTML={this.createMarkup(this.state.blogObject.content)} /> 
         </div> 
        </div> 

       : <div>Loading...</div> 

       } 

      </div> 

     ) 

    } 
} 

export default Post 

或者與導航到一個頁面後我博客列表按鈕操作

<Link id={this.props.blogObject.slug} to={`/post/${this.props.blogObject.slug}`}> 
    <button className='blog-button' style={buttonStyle} id={this.props.blogObject.slug} /> 
</Link> 

回答

0

我不知道爲什麼它會繼續使整個組件,但如果我得到的post組件擺脫<NavBar />的問題就會消失。

不工作

return (

      <div> 
       <NavBar /> 

       {this.state.blogObject ? 

        <div className={postContainerClasses} > 
         <div className='header-image' style={this.assignStyleToCoverImage(this.state.blogObject)} /> 
         <div className='post-body-padding'> 
          <div className='post-header-info'> 
           <h1 className='post-title'>{this.state.blogObject.title}</h1> 
           <h4 className='post-date'>{this.formatDateString(this.state.blogObject.created_at)}</h4> 
          </div> 
          <div className={postBodyClasses} dangerouslySetInnerHTML={this.createMarkup(this.state.blogObject.content)} /> 
         </div> 
        </div> 

       : <div>Loading...</div> 

       } 

      </div> 

     ) 

工作

return (

      <div> 

       {this.state.blogObject ? 

        <div className={postContainerClasses} > 
         <div className='header-image' style={this.assignStyleToCoverImage(this.state.blogObject)} /> 
         <div className='post-body-padding'> 
          <div className='post-header-info'> 
           <h1 className='post-title'>{this.state.blogObject.title}</h1> 
           <h4 className='post-date'>{this.formatDateString(this.state.blogObject.created_at)}</h4> 
          </div> 
          <div className={postBodyClasses} dangerouslySetInnerHTML={this.createMarkup(this.state.blogObject.content)} /> 
         </div> 
        </div> 

       : <div>Loading...</div> 

       } 

      </div> 

     ) 
相關問題