React 的获取搜索和分页问题
Problem with a fetch search and pagination with React
我正在用 React 构建一个电影项目,我已经创建了分页和搜索栏。当我进行搜索时,一切正常,我显示了正确的页数,但是当我点击一个数字来更改页面时,没有任何反应。
这是分页组件:
// == Import
import { createTheme, Pagination, ThemeProvider } from '@mui/material';
import PropTypes from 'prop-types';
import { PaginationStyle } from './CustomPaginationStyles';
const theme = createTheme({
palette: {
mode: 'dark',
},
});
// == Composant
const CustomPagination = ({ setPage, numOfPages }) => {
const handlePageChange = (page) => {
setPage(page);
// Scroll qui remonte lors du changement de page
window.scroll(0, 0);
};
return (
<ThemeProvider theme={theme}>
<PaginationStyle>
<Pagination
count={numOfPages}
color="primary"
size="large"
onChange={(event) => handlePageChange(event.target.textContent)}
hideNextButton
hidePrevButton
/>
</PaginationStyle>
</ThemeProvider>
);
};
CustomPagination.propTypes = {
setPage: PropTypes.func.isRequired,
numOfPages: PropTypes.number.isRequired
};
CustomPagination.defaultProps = {
numOfPages: 20
};
// == Export
export default CustomPagination;
这是搜索组件:
// == Import
import { HomeTitle, Container } from '../../Styles/globalStyles';
import SearchIcon from '@mui/icons-material/Search';
import baseUrl from '../../Redux/baseUrl';
import './SearchBar.scss';
import { useEffect, useState } from 'react';
import SingleContent from '../../components/Content/SingleContent';
import CustomPagination from '../../components/CustomPagination/CustomPagination';
// == Composant
const Search = () => {
const [searchText, setSearchText] = useState('');
const [page, setPage] = useState(1);
const [content, setContent] = useState([]);
const [numOfPages, setNumOfPages] = useState();
const handleOnChange = (event) => {
setSearchText(event.target.value);
};
const fetchData = async () => {
try {
const response = await baseUrl.get(`search/multi?api_key=${process.env.REACT_APP_API_KEY}&language=en-US&query=${searchText}&page=${page}&include_adult=false`);
console.log(response);
setContent(response.data.results);
setPage(response.data.page);
setNumOfPages(response.data.total_pages);
setSearchText('');
} catch (error) {
console.trace(error);
}
};
const handleOnSubmit = (event) => {
// on va tester s'il y a assez de lettre
if (searchText.length < 3) {
alert('Veuillez mettre au moins trois caractères');
}
else {
event.preventDefault();
fetchData();
}
};
useEffect(() => {
}, [searchText, page]);
return (
<div>
<HomeTitle>Search</HomeTitle>
<div className='search-bar'>
<form
className='search'
onSubmit={handleOnSubmit}
>
<input
type="text"
placeholder="Search"
value={searchText}
onChange={handleOnChange}
/>
<button type='submit' className='search-icon'><SearchIcon/></button>
</form>
</div>
<Container>
{content &&
content.map((searchContent) => (
<SingleContent
key={searchContent.id}
id={searchContent.id}
poster={searchContent.poster_path}
title={searchContent.title || searchContent.name}
date={searchContent.first_air_date || searchContent.release_date}
media_type={searchContent.media_type}
vote_average={searchContent.vote_average}
/>
))}
{searchText && !content && (<h2>No Series Found</h2>)}
</Container>
{numOfPages > 1 && (<CustomPagination setPage={setPage} numOfPages={numOfPages} />)}
</div>
);};
// == Export
export default Search;
当页面状态更改时,您似乎需要再进行一次 api 调用。我在搜索组件中看到你已经有一个像这样的 useEffect 挂钩
useEffect(() => {
}, [searchText, page]);
尝试在已侦听页面值更改的挂钩中调用您的 fetchData 函数:
useEffect(() => {
fetchData()
}, [searchText, page]);
我正在用 React 构建一个电影项目,我已经创建了分页和搜索栏。当我进行搜索时,一切正常,我显示了正确的页数,但是当我点击一个数字来更改页面时,没有任何反应。
这是分页组件:
// == Import
import { createTheme, Pagination, ThemeProvider } from '@mui/material';
import PropTypes from 'prop-types';
import { PaginationStyle } from './CustomPaginationStyles';
const theme = createTheme({
palette: {
mode: 'dark',
},
});
// == Composant
const CustomPagination = ({ setPage, numOfPages }) => {
const handlePageChange = (page) => {
setPage(page);
// Scroll qui remonte lors du changement de page
window.scroll(0, 0);
};
return (
<ThemeProvider theme={theme}>
<PaginationStyle>
<Pagination
count={numOfPages}
color="primary"
size="large"
onChange={(event) => handlePageChange(event.target.textContent)}
hideNextButton
hidePrevButton
/>
</PaginationStyle>
</ThemeProvider>
);
};
CustomPagination.propTypes = {
setPage: PropTypes.func.isRequired,
numOfPages: PropTypes.number.isRequired
};
CustomPagination.defaultProps = {
numOfPages: 20
};
// == Export
export default CustomPagination;
这是搜索组件:
// == Import
import { HomeTitle, Container } from '../../Styles/globalStyles';
import SearchIcon from '@mui/icons-material/Search';
import baseUrl from '../../Redux/baseUrl';
import './SearchBar.scss';
import { useEffect, useState } from 'react';
import SingleContent from '../../components/Content/SingleContent';
import CustomPagination from '../../components/CustomPagination/CustomPagination';
// == Composant
const Search = () => {
const [searchText, setSearchText] = useState('');
const [page, setPage] = useState(1);
const [content, setContent] = useState([]);
const [numOfPages, setNumOfPages] = useState();
const handleOnChange = (event) => {
setSearchText(event.target.value);
};
const fetchData = async () => {
try {
const response = await baseUrl.get(`search/multi?api_key=${process.env.REACT_APP_API_KEY}&language=en-US&query=${searchText}&page=${page}&include_adult=false`);
console.log(response);
setContent(response.data.results);
setPage(response.data.page);
setNumOfPages(response.data.total_pages);
setSearchText('');
} catch (error) {
console.trace(error);
}
};
const handleOnSubmit = (event) => {
// on va tester s'il y a assez de lettre
if (searchText.length < 3) {
alert('Veuillez mettre au moins trois caractères');
}
else {
event.preventDefault();
fetchData();
}
};
useEffect(() => {
}, [searchText, page]);
return (
<div>
<HomeTitle>Search</HomeTitle>
<div className='search-bar'>
<form
className='search'
onSubmit={handleOnSubmit}
>
<input
type="text"
placeholder="Search"
value={searchText}
onChange={handleOnChange}
/>
<button type='submit' className='search-icon'><SearchIcon/></button>
</form>
</div>
<Container>
{content &&
content.map((searchContent) => (
<SingleContent
key={searchContent.id}
id={searchContent.id}
poster={searchContent.poster_path}
title={searchContent.title || searchContent.name}
date={searchContent.first_air_date || searchContent.release_date}
media_type={searchContent.media_type}
vote_average={searchContent.vote_average}
/>
))}
{searchText && !content && (<h2>No Series Found</h2>)}
</Container>
{numOfPages > 1 && (<CustomPagination setPage={setPage} numOfPages={numOfPages} />)}
</div>
);};
// == Export
export default Search;
当页面状态更改时,您似乎需要再进行一次 api 调用。我在搜索组件中看到你已经有一个像这样的 useEffect 挂钩
useEffect(() => {
}, [searchText, page]);
尝试在已侦听页面值更改的挂钩中调用您的 fetchData 函数:
useEffect(() => {
fetchData()
}, [searchText, page]);