reactjs - ReactJS - 传递参数以获取请求
问题描述
以下是显示所有类别的 React 组件,在此路由 http://localhost:3000/categories
import React, { useEffect, useState } from 'react';
import { useDispatch, useSelector } from "react-redux";
import { Link } from "react-router-dom";
// Material-UI core
import Grid from '@material-ui/core/Grid';
import Container from '@material-ui/core/Container';
// Local Components
import BodyWrapper from 'components/bodyWrapper';
import Spinner from 'components/progressAnimations/spinner';
import CategoryCard from 'components/categoryCard';
import { CARD1, CARD2, CARD3 } from '../../constants'; // images
import { useStyles } from './style';
function Categories(props) {
const classes = useStyles();
const loading = useSelector(state => state.categories.isLoading);
const categories = useSelector(state => state.categories.categories);
return (
<BodyWrapper spacing = "40px" containerWidth = "lg">
<h2>Categories</h2>
<Grid container spacing={4}>
{
!loading ?
categories.data[0].documents.map((category, index) => (
<Grid key={index} item xs={6} md={4} lg={3} >
<Link to = {`/categories/${category.name}`} style={{ textDecoration: "none" }}>
<CategoryCard image={ category.imageUrl || CARD2 } title={ category.name } />
</Link>
</Grid>
)) : (
<div className = { classes.spinner }>
<Spinner />
</div>
)
}
</Grid>
</BodyWrapper>
);
}
export default Categories;
当单击特定类别时,我想加载一个新组件并创建一个 GET 请求到具有类别 ID 的服务器以加载与类别相关的所有数据。
http://localhost:3000/categories/Photographers
这是 API URL,我想通过 GET 请求发送“类别 ID”。
{{SERVER}}/api/vendor/getAll?page=1&limit=10&categories=5fdf4d9db1ecf930cd09331b
我想知道将类别 ID 传递给下一个组件的最佳方法。
解决方案
你可以这样:
import React, { useEffect, useState } from "react";
import axios from "axios";
const Categories = (props) => {
// we get something from Redux
const categories = ["a", "b", "c", "d", "e", "f"];
const [currentCategory, setCurrentCategory] = useState(null);
return props.loading ? (
"Loading..."
) : (
<div>
<h2>Select category</h2>
{categories.map((category) => (
<div key={category} onClick={() => setCurrentCategory(category)}>
Category: {category}
</div>
))}
<div>current category is {String(currentCategory)}</div>
{currentCategory && <Category id={currentCategory} />}
</div>
);
};
const Category = ({ id }) => {
const [data, setData] = useState(null);
const url = "https://someurl.com";
useEffect(() => {
const fetchData = async () => {
const dataFromServer = await axios.get(`${url}/${id}`);
setData(dataFromServer);
};
fetchData();
}, [id]);
return data ? (
<div>Render data here: {data}</div>
) : (
`Loading category ${id}...`
);
};
const App = () => {
return (
<div>
<Categories />
</div>
);
};
export default App;
推荐阅读
- c# - 我如何重用一个方法而不是创建多个副本
- gstreamer - Gstreamer:将 rtsp 流中继到另一台 Windows 计算机
- python - Qt Quick 与 QtWidgets 风格相同
- c# - 基于 UserRole 隐藏数据表 ActionButton 编辑和删除
- javascript - 更改 sweetalert 弹出窗口中的标题位置并使按钮 sweetalert2 处于非活动状态
- ios - 当视图消失以进行内存管理时如何删除视图?
- python - 为什么我的代码不会遍历 for 循环中的每个元素?
- python - 连接表 SQLAlchemy 中的复杂过滤器
- python - 如何使用索引执行熊猫数据帧子集并为其分配相同的值?
- python - 将图像从 matplotlib.pyplot 显示到 tensorflow 时出现混淆