【问题标题】:Map of an array in a functional component and access to a specific object功能组件中数组的映射以及对特定对象的访问
【发布时间】:2020-02-16 15:15:08
【问题描述】:
function TypeArticleOne(props) {
let apiData = props.apiData;
const [ therapists, setTherapists ] = useState(apiData.topProfiles.therapists);
const [speciality, setSpeciality]= useState('ABA');
const [pageLoading, setPageLoading]= useState(true);

const topProfilesUrl = 'therapists/top/profiles'

useEffect(() => {
    console.log(speciality);
    getTopTherapists();
    window.scrollTo(0, 0);

}, []);

const getTopTherapists = () => {
    setPageLoading(true);
    loadTopTherapists();

};

const loadTopTherapists = () => {
    console.log("second");
    props.actions.reqGetTherapistsTopProfiles({
        body: {},
        headers: null,
        resource: `${topProfilesUrl}`
    })
};

useEffect(() => {

    if (apiData.topProfiles && apiData.topProfiles.success) {
        const therapistsLoad = apiData.topProfiles.therapists;
        setPageLoading(false);
        setTherapists([therapists].concat(therapistsLoad));
    }
    }, []);

如何在功能组件中映射数组?我想从上面的功能组件映射治疗师数组。 React 建议我使用 UseRef(),因为我有一个功能组件并且我正在使用钩子,但我不清楚。 我从数据库中调用数组中的治疗师,我需要将它们映射到功能组件内的卡片中呈现。现在我可以访问数组元素,但我需要访问对象的一些特定参数。你们能帮帮我吗?

const renderTherapists = (props) => {
    const items = props.therapists.map( (t, idx) => (
        <TherapistCard therapist={t} key={idx} />
    ))

    return (
        <div ref={0} className="therapist-list">
            { items }
        </div>
    )
}

【问题讨论】:

    标签: arrays reactjs react-hooks


    【解决方案1】:

    与其声明一个const,不如直接映射props这样:

    const RenderTherapists = props => {
        return (
            <div className="therapist-list">
             { props.therapists.map((t, idx) => {
                   return <TherapistCard therapist={t} key={idx} />
             })}
            </div>
        )
    }
    
    export default RenderTherapists;
    

    【讨论】:

      猜你喜欢
      • 2021-02-09
      • 1970-01-01
      • 2023-04-08
      • 2017-08-23
      • 2023-03-17
      • 1970-01-01
      • 2022-06-17
      • 2017-08-18
      • 2012-03-05
      相关资源
      最近更新 更多