为了账号安全,请及时绑定邮箱和手机立即绑定

如果反应嵌套渲染

如果反应嵌套渲染

GCT1015 2022-01-07 16:32:26
嘿,想在渲染方法中做一个嵌套的 if我的代码:return(    <>    <Logo className='logo' />    <Menu loadPage={loadPage} />    {load.favorite ?       <Favorite         favorite={favorite}         addRemoveFavorite={addRemoveFavorite}        getPersonMovie={getPersonMovie}      />       :      <>      <Card         people={people}         favorite={favorite}         addRemoveFavorite={addRemoveFavorite}        getPersonMovie={getPersonMovie}      />      <Navigate         onClick={onClick}        page={page}       />       </>    }    </>    )}我想做一些这样的想法return(    <>    {load.page     ?     <Img />    :     rest code }不幸的是,该代码不起作用,我不知道为什么。有人可以帮忙吗?
查看完整描述

2 回答

?
慕村9548890

TA贡献1884条经验 获得超4个赞

实际上你的代码会load.page ?改为load.favorite......但你可以清楚地为你想要的东西设置一个钩子......例如:


const [loadImg, setLoadImg] = useState(true);


useEffect(() => {

    async function done....

    setLoadImg(false);

}, [])


...    {loadImg ? <ComponentThis /> : <ComponentThat /> }


查看完整回答
反对 回复 2022-01-07
?
www说

TA贡献1775条经验 获得超8个赞

import React, {useEffect, useState} from 'react';

import API_URL from './config/API_URL';

import Card from './components/Card';

import Favorite from './components/Favorite';

import Menu from './components/Menu';

import Navigate from './components/Navigate';

import { ReactComponent as Logo} from './logo.svg';

import './scss/App.scss';


const App = () => {

  const [people, setPeople] = useState([]);

  const [films, setFilms] = useState([]);

  const [favorite, setFavorite] = useState([]);

  const [load, setLoad] = useState({favorite: false, page: true });

  const [page, setPage] = useState({next: {}, previous: {}})


  useEffect(() =>{

    (async () => {

      const data = await fetchData(API_URL) 

      const people = await fetchData(data.people)

      const films = await fetchData(data.films)


      setPeople(people.results);

      setFilms(films.results);

      setPage({next: people.next, previous: people.previous});

      setLoad({page: false});

    })()    

  }, [])


  async function fetchData(url) {

    const response = await fetch(url);

    return response.json();

  };


  const getPersonMovie = item => films.filter(film => item.films.includes(film.url))


  const onClick = URL => {

    setLoad({page: true});

    if(URL !== null) {

      fetchData(URL)

        .then(res => {

          setPeople(res.results);

          setPage({next: res.next, previous: res.previous});

          setLoad({page: false});

        })

    }

  }


  const addRemoveFavorite = (item) => {

    if(favorite.includes(item)) {

      const newArr = favorite.filter(el => el !== item)

      setFavorite(newArr);

    } else {

      setFavorite([...favorite, item])

    }

  }

  const loadPage = el => {

    setLoad({favorite: el});

  }




  return(

    <>

    <Logo className='logo' />

    <Menu loadPage={loadPage} />

    {load.favorite ? 

      <Favorite 

        favorite={favorite} 

        addRemoveFavorite={addRemoveFavorite}

        getPersonMovie={getPersonMovie}

      /> 

      :

      <>

      <Card 

        people={people} 

        favorite={favorite} 

        addRemoveFavorite={addRemoveFavorite}

        getPersonMovie={getPersonMovie}

      />

      <Navigate 

        onClick={onClick}

        page={page}

       />

       </>

    }

    </>  

  )

}


export default App;


查看完整回答
反对 回复 2022-01-07
  • 2 回答
  • 0 关注
  • 142 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信