2017-10-11 2 views
1

반응 라우터를 사용하려고하는데 문제가 있습니다. 링크를 클릭하면 이전 데이터가 사라지고 현재 데이터 만 표시됩니다. 하지만 내 경우에는 두 데이터로 표시되며 또한 URL을 통해 액세스 할 수 없습니다. 누군가 나를 도울 수 있습니까?React router show data

반응 라우터를 사용하려고하는데 문제가 있습니다. 링크를 클릭하면 이전 데이터가 사라지고 현재 데이터 만 표시됩니다. 하지만 내 경우에는 두 데이터로 표시되며 또한 URL을 통해 액세스 할 수 없습니다. 누군가 나를 도울 수 있습니까?

내 코드 :

import React, {Component} from 'react' 
 
import axios from 'axios' 
 

 
import ShotList from './shotList' 
 

 

 
const URL = 'https://api.dribbble.com/v1/shots/' 
 
const token = 'token' 
 

 
export default class Shots extends Component{ 
 

 
    constructor(props){ 
 
     super(props) 
 
     this.state = {list: []} 
 
     
 
     this.getShots() 
 
    } 
 
    
 
    getShots(){ 
 
     axios.get(`${URL}?access_token=${token}`) 
 
     .then(resp => this.setState({...this.state, list: resp.data}))  
 
    } 
 

 
    render(){ 
 
     return(
 
      <div> 
 
       
 
       <ShotList list={this.state.list}/> 
 
       
 
       </div> 
 
     ) 
 
    } 
 
}

import React from 'react' 
 
import {BrowserRouter as Router, Route, Link} from 'react-router-dom' 
 

 
export default props =>{ 
 
    
 

 
    const renderRows =() =>{ 
 
     const list = props.list || [] 
 
     JSON.stringify(list) 
 
     return list.map(shots =>(
 
     <Router key={shots.id}> 
 
      <div > 
 
      <Link to={`/shots/${shots.id}`}> 
 
       <div className='col-md-3 col-sm-4 col-xs-6 teste'> 
 
        <img src={shots.images.normal} className='img-responsive' /> 
 
        <p>{shots.views_count}</p> 
 
       </div> 
 
       </Link> 
 
       <Route path="/shots/:shotsId" component={Detail}/> 
 
       </div> 
 
        
 
     </Router> 
 

 
     
 
     )) 
 
     
 
     
 
} 
 

 
const Detail = ({match}) =>{ 
 
    
 
    return(
 
     <div> 
 
      {match.params.shotsId} 
 
      
 
      </div> 
 
    ) 
 
    
 

 
} 
 

 
    return(
 
     <div> 
 
      {renderRows()} 
 
     </div> 
 
    ) 
 
     
 

 
}

답변

0

는 대신 각 행에 대해 새로운 Router을 만드는, 단 1 Router 라우팅이 필요합니다. 선택된 Route 만 렌더링하려면 Switch을 사용해야합니다. renderRows의 코드는 다음과 같이 수정할 수 있습니다.

import React from 'react' 
 
import {BrowserRouter as Router, Route, Switch, Link} from 'react-router-dom' 
 

 
export default props =>{ 
 
    const renderRows =() =>{ 
 
    const list = props.list || [] 
 
    JSON.stringify(list) 
 
    return (
 
    <Router> 
 
     <div> 
 
     {list.map(shots => 
 
      <Link to={`/shots/${shots.id}`}> 
 
      <div className='col-md-3 col-sm-4 col-xs-6 teste'> 
 
       <img src={shots.images.normal} className='img-responsive' /> 
 
       <p>{shots.views_count}</p> 
 
      </div> 
 
      </Link>) }   
 
     
 
     <Switch> 
 
      {list.map(shots =><Route path="/shots/:shotsId" component={Detail}/>)} 
 
     </Switch> 
 
     </div>     
 
     </Router>); 
 
}

편집 : 의견은 수신 된 코드에서 수정 된 오류 후.

+0

이제이 오류를 보여줍니다.
app.js : 9234 Uncaught Error : 구성 요소 (...) : 유효한 React 요소 (또는 null)가 반환되어야합니다. undefined, 배열 또는 다른 잘못된 객체를 반환했을 수 있습니다. –

+0

코드를 업데이트했습니다. 지금 확인하십시오. – palsrealm

+0

나는 내 코드도 편집했는데, 그 부분을 잃어 버렸지 만 그것을 변경하려고 시도했지만 작동하지 않았다. 그것은 내가 지금 넣은 다른 코드와 관련이 있습니까? –