首页 > 解决方案 > 无法访问显示未定义的反应应用程序中的道具

问题描述

因此,当我将道具从我的 Next Js 页面之一传递到我的组件时,我可以控制台记录道具并且它包含我想要的数据,然后我将这些数据向下传递给各个组件,但我无法注销子元素道具,因为它们显示为未定义。可能与 fetch 及其异步行为有关。

任何帮助。

下一个 Js 页面:

import React, { useContext } from 'react';
import Layout from '../components/globals/Layout';
import components from '../static-data/components';
import IncludeComponents from '../include-components';
import DataContext from '../DataContext';
import StoreDirectory from '../components/StoreDirectory';

const Stores = (props) => {
    const { pageData } = useContext(DataContext);

    return (   
        <Layout>
            <StoreDirectory props={props}/>
            {/* <IncludeComponents components={pageData.storesPage} /> */}
        </Layout>
    )
}

export async function getStaticProps() {

    const resStores = await fetch("http://caladonia-park-cms.betastaging.co.uk/wp-json/wp/v2/stores")
    const storesData = await resStores.json();

    const resStoresCategorys = await fetch("http://caladonia-park-cms.betastaging.co.uk/wp-json/wp/v2/store-category")
    const storesCategorysData = await resStoresCategorys.json();

    return {
        props: {
            storesData,
            storesCategorysData
        }
    }
}

export default Stores;

商店目录:

import React, { useRef } from 'react';
// import { data } from './data/data';
import { useState, useEffect } from 'react';
import Form from 'react-bootstrap/Form';
import ScrollToTop from './ScollToTop';

const DropDown = (props) => {
    console.log(props.storesData.id)

    const [storeCategorys, setStoreCategories] = useState(props.storesCategorysData);
    const [initialValues, setInitialValues] = useState(props.storesData);

    const handleOnchange = (event) => {
        const filter = event.target.id;
        const initialState = [...initialValues]
        setValues(initialState.filter(store => 
            {return (store.store-category.indexOf(filter) >= 0)}
        ));
    }

    const defaultSelectMessage = 'select a category';

    return (
        <Form>
            <Form.Group controlId="exampleForm.SelectCustom">
            <Form.Label>Custom select</Form.Label>
            <Form.Control defaultValue={defaultSelectMessage} onChange={(e) => handleOnchange(e)} as="select" custom>
                <option hidden disabled value={defaultSelectMessage}>{defaultSelectMessage}</option>
                {storeCategorys.map((item, index) => (
                <option id={item.id} key={index}>{item}</option>
                ))}
            </Form.Control>
            </Form.Group>
        </Form>
    );
}

const Filter = (props) => {
    return (
        <div>
            {props.alphabet.split("").map((c, i) => {
                return (
                    <>
                    {props.filteredValues
                    .filter(store => store.title.rendered.startsWith(c)).length === 0 
                    ? <h1 ref={ el => props.itemsRef.current[i] = el } className={'Grey'}>{c}</h1>
                    : <h1 ref={ el => props.itemsRef.current[i] = el } className={'notGrey'}>{c}</h1>   
                    }
                    
                    {props.filteredValues
                    .filter(store => store.title.rendered.startsWith(c))
                    .map((item, index) => (   
                        <li key={index}>{item.title.rendered}</li>
                    ))}
                    
                    </>
                );
            })}
        </div>
    );
}

const AlphaButtons = (props) => {

    const alphabet = "abcdefghijklmnopqrstuvwxyz";
    const alphabetIntoArray = alphabet.split("");

    const itemsRef = useRef([]);

    useEffect(() => {
        itemsRef.current = itemsRef.current.slice(0, alphabetIntoArray.length);
    }, [alphabetIntoArray])

    const goToSelection = (event, index) => {
        if(itemsRef.current[index]) {
            itemsRef.current[index].scrollIntoView({
            behaviour: "smooth",
            block: "nearest"
            })
        }
    }

    return (
    <>
        {alphabet.split("").map((item, index) => (
            <button key={index} onClick={(e) => goToSelection(e, index)}>{item}</button>
        ))}  

        <Filter filteredValues={filteredValues} alphabet={alphabet} itemsRef={itemsRef}/>    
    </>
    )
}

const StoreDirectory = (props) => {
    const [filteredValues, setValues] = useState(props.storesData);

    console.log(props)
    

    return (
        <>
            <DropDown props={props}/>
            <AlphaButtons filteredValues={filteredValues}/> 
            <ScrollToTop />
        </>
    )

}

export default StoreDirectory;

我可以在这里记录道具,但是当我尝试访问孩子时什么也没有。

标签: javascriptreactjsnext.js

解决方案


推荐阅读