해당 카테고리와 관련된 제품의 제품 색인 페이지로 연결되는 카테고리 색인 페이지가 있습니다. 그만큼 기능하고 있습니다. 그러나 해당 제품의 특정 쇼 구성 요소에 연결된 제품을 클릭하려고하면 문제가 발생합니다.반응 라우터로 상대 경로 중첩
router.js
import React from 'react';
import { Router, Route, Switch } from 'react-router';
import createBrowserHistory from 'history/createBrowserHistory'
import App from './App';
import CategoriesIndexPage from './pages/categories/CategoriesIndexPage';
import ProductsIndexPage from './pages/products/ProductsIndexPage';
import ProductShow from './pages/products/ProductShow';
import LocationsPage from './pages/LocationsPage';
const history = createBrowserHistory()
const router = (
<Router history={history}>
<Switch>
<Route exact path='/' component={App}/>
<Route path='/categories' component={CategoriesIndexPage}/>
<Route path='/locations' component={LocationsPage}/>
<Route path='/:category' component={ProductsIndexPage}>
<Route path='/:id' component={ProductShow}/>
</Route>
</Switch>
</Router>
);
export default router;
ProductIndexPage.js
import React, { Component } from 'react';
import { BWReactData } from '../../config/FirebaseConstants.js';
import Head from '../../components/Head.js';
import Foot from '../../components/Foot.js';
import ProductsIteration from './ProductsIteration';
class ProductsIndexPage extends Component {
constructor(props){
super(props);
this.state = {
allProducts: [],
loading: true,
}
}
componentDidMount() {
...
}
render() {
let allProducts = this.state.allProducts;
let loading = this.state.loading;
let categoryURL = this.props.location.state.category;
return (
<div>
<Head/>
<ProductsIteration
allProducts={allProducts}
loading={loading}
categoryURL={categoryURL}
/>
<Foot/>
</div>
)
}
}
export default ProductsIndexPage;
ProductsIteration.js
import React from 'react';
import { Link } from 'react-router-dom';
import { Col, Row } from 'react-materialize';
const ProductsIteration = props => {
let category = props.categoryURL;
if (props.loading) {
return <div>Loading...</div>
}
return (
<Row>
{props.allProducts.map(function(object) {
return (
<Col s={12} m={6} l={3} key ={object.id}>
<div style={styles.wrapper}>
<Link to={{ pathname: `${category}/${object.id}`, state: { id: object.id }}}>
<img src={object.img} style={styles.image} />
<div style={styles.description}>
<div style={styles.descriptionContent}>{object.name}</div>
</div>
</Link>
</div>
</Col>
)
})}
</Row>
)
}
export default ProductsIteration;
내 반복 구성 요소 내의 링크를 렌더링하는 '/ : 다음은 내 코드입니다 : category/: id '내 navbar에있는 URL이지만 페이지는 아무 것도하지 않습니다. 이것은 라우터를 사용하는 나의 첫 번째 프로젝트이며 모든 지침을 많이 주시면 감사하겠습니다.
감사합니다,하지만 페이지에 렌더링하는 ProductIndexPage과 ProductShow 구성 요소를 일으키는 구성으로 동시에 다른 하나 아래 하나 – mcw
좋습니다. 'id' 매개 변수가있을 때만' '컴포넌트를 보여주기 위해 코드를 수정했습니다. 그러나 나는 당신이 단지 하나 또는 다른 것을 보여주고 싶을 것 같은 느낌을 갖습니다. 그렇다면 :' '' '구성 요소에'exact' 소품을 넣고 그 아래에 다른' '구성 요소를'path = "/ : category/: id"특별히 '구성 요소. 상위 ' '구성 요소가 나머지를 처리합니다. –
훌륭합니다. 당신의 도움을 주셔서 감사합니다! – mcw