上下文 API 未显示数据数组

Context API not displaying array of data

我收到错误“期望函数调用赋值,却看到了一个表达式”。

*上面的错误已经解决,现在给我一个未处理的拒绝(TypeError):render is not a function。它运行正常,直到我进入产品页面然后给我错误。

我在控制台记录了它,它正在提取信息,但是当我导入 ProductBrowse 组件以显示信息时它中断了。

产品页面:

class ProductPage extends React.Component {
  state = {
    shoppingCartOpen: false,
  };
  drawerToggleClickHandler = () => {
    this.setState((prevState) => {
      return { shoppingCartOpen: !prevState.shoppingCartOpen };
    });
  };

  render() {
    let shoppingCartDrawer;
    if (this.state.shoppingCartOpen) {
      shoppingCartDrawer = <ShoppingCartDrawer />;
    }
    return (
      <ProductStyling>
        <ButtonAppBar drawerClickHandler={this.drawerToggleClickHandler} />
        <H1>Products</H1>
        {shoppingCartDrawer}
        <ProductConsumer>
          <Grid container spacing={3}>
            {(value) => {
              return value.products.map((prod, idx) => {
                return (
                  <Grid item xs={3} key={`${prod.name}-${prod.store}-${idx}`}>
                    <ProductBrowse
                      productName={prod.name}
                      productDesc={prod.desc}
                      productImg={prod.img}
                      productPrice={prod.price}
                    />
                  </Grid>
                );
              });
            }}
          </Grid>
        </ProductConsumer>
        ;
      </ProductStyling>
    );
  }
}

值的结构:

const ProductContext = React.createContext();

class ProductProvider extends React.Component {
state = {
products: productData, 
};

addToCart = () => {
console.log('Hello from add to cart'); };

render() {
return (
  <ProductContext.Provider
    value={{ ...this.state, addToCart: this.addToCart }}
  >
    {this.props.children}
  </ProductContext.Provider>
); 
}
}

const ProductConsumer = ProductContext.Consumer;

export { ProductProvider, ProductConsumer };

产品浏览:

const ProductBrowse = ({
      productName,
      productDesc,
      productImg,
      productPrice,
    }) => {
      const classes = useStyles();
      const [open, setOpen] = React.useState(false);
    
      const openModal = () => {
        setOpen(!open);
      };
    
      const closeModal = () => {
        setOpen(!open);
      };
    
      return (
        <Box border={1} borderRadius={3}>
          <Card className={classes.root}>
            <CardActionArea onClick={() => openModal()}>
              <ProductModal
                open={open}
                onClick={() => openModal()}
                onClose={() => closeModal()}
                onSave={() => closeModal()}
                productName={productName}
                productDesc={productDesc}
              />
              <CardHeader
                title={productName}
                subheader={formatCurrency(productPrice)}
              />
              <CardMedia
                className={classes.media}
                image={productImg}
                alt={productDesc}
              />
              <CardContent>
                <Typography variant='body2' color='textSecondary' component='p'>
                  {productDesc}
                </Typography>
              </CardContent>
            </CardActionArea>
            <CardActions>
              <Button size='small' /*To Checkout*/>BUY NOW</Button>
              <Button size='small'>ADD TO CART</Button>
              <Button size='small'>REVIEW</Button>
            </CardActions>
          </Card>
        </Box>
      );
    };

产品数据:

import desk from '../../../assets/img/desk.webp'; 

export const productData = [
    {
      img: desk,
      name:  'Desk',
      store: 'Local Furniture Shop 1',
      price: 9.99, 
      desc: "This sturdy desk is built to outlast years of coffee and hard work. You get a generous work surface and a clever solution to keep cords in place underneath." 
    },

Index.js:

ReactDOM.render(
  <React.StrictMode>
    <Auth0Provider
      domain={auth0Domain}
      client_id={auth0ClientID}
      redirect_uri={window.location.origin}
      onRedirectCallback={onRedirectCallback}
      audience={auth0Audience}
      scope={"read:current_user"}
    >
      <ProductProvider>
      <Provider store={store}>
        <App />
      </Provider>
      </ProductProvider>
    </Auth0Provider>
  </React.StrictMode>,
  document.getElementById('root')
);

您不会从 ProductConsumer 返回任何内容 您需要这样做:

<ProductConsumer>
  <Grid container spacing={3}>
    {(value) => {
      return value.products.map((prod, idx) => {
        return (
          <Grid item xs={3} key={`${prod.name}-${prod.store}-${idx}`}>
            <ProductBrowse
              productName={prod.name}
              productDesc={prod.desc}
              productImg={prod.img}
              productPrice={prod.price}
            />
          </Grid>
        );
      });
    }}
  </Grid>
</ProductConsumer>;