我有三种方法来显示画廊(所有画廊,作者画廊和活跃用户画廊)。最后一种不起作用。
版面配置页面。
function Layout() {
const activeUser = useSelector(userSelector);
const isUserAuthenticated = useSelector(isAuthenticated);
const dispatch = useDispatch();
useEffect(() => {
if(isAuthenticated){
dispatch(getActiveUser())
}
},[])
<Route exact path="/galleries">
<GalleriesApp />
</Route>
<Route exact path="/authors/:id">
<GalleriesApp />
</Route>
<Route exact path="/my-galleries">
<GalleriesApp my_id={isUserAuthenticated ? activeUser.id : null}/>
</Route>
字符串
对于我的画廊路径,我发送的活动用户id作为 prop ,它得到发送时,用户点击链接,但我它显示相同的页面作为主要的画廊应用程序。
图库应用程序页面。
function GalleriesApp({my_id = null}) {
const dispatch = useDispatch();
const galleries = useSelector(selectGalleries);
const term = useSelector(selectSearchTerm);
const user_id = useSelector(selectSearchUserId);
const { id } = useParams();
useEffect(() => {
dispatch(getActiveUser(id))
},[id])
console.log(my_id);
console.log(id);
useEffect(() => {
if(my_id){
dispatch(setSearchUserId(id));
dispatch(getGalleries({ page: 1, user_id: my_id, term: null}));
}
if(id){
dispatch(setSearchUserId(id));
dispatch(getGalleries({ page: 1, user_id: id, term: null}));
}
else{
dispatch(setSearchUserId(null));
dispatch(getGalleries({ page: 1, user_id: null, term: null}));
}
}, [my_id, id, dispatch]);
function handlePaginate(page) {
if(my_id){
dispatch(getGalleries({ page: page, user_id: my_id, term: term}));
}
if(id){
dispatch(getGalleries({ page: page, user_id: id, term: term}));
}else{
dispatch(getGalleries({ page: page, user_id: null, term: term}));
}
}
function handleSearchTerm(e) {
dispatch(setSearchTerm(e.target.value));
}
function handleSearch() {
dispatch(getGalleries({ page: 1, user_id: user_id, term: term}))
}
return (
<div>
<div>
<h1>{my_id && 'My '}{id && galleries.data.length ? (`${galleries?.data[0]?.user?.first_name}'s `)
: (<></>)}
Galleries </h1>
{galleries.data.length ? (
<div>
<GallerySearch
handleSearchTerm={handleSearchTerm}
handleSearch={handleSearch}
/>
<ul>
{galleries.data.map((gallery) =>(
<GalleryRow key={gallery.id} gallery={gallery}/>
))}
</ul>
{galleries.current_page !== galleries.last_page && (
<button onClick={() => handlePaginate(galleries.current_page + 1)}>
Load More
</button>
)}
</div>
) : (
<div>There are no galleries</div>
)}
</div>
</div>
)
}
型
我希望在进入我的画廊时得到活跃的用户画廊。即使id成功地通过 prop 传递,我也得到了完全相同的画廊应用页面。
1条答案
按热度按时间ql3eal8s1#
您的
useEffect()
有逻辑问题。此代码:字符串
将在
/galleries
和/my-galleries
两种情况下都命中else
。对于/my-galleries
,它将首先运行if (my_id)
块,但随后也将运行else
块,从而否定在if (my_id)
块中完成的工作。我想你的情况会得到解决,如果你改变代码:
型
(see
else if
!!)