首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React Firebase Cart Add Items Cart Reducer不工作

React Firebase Cart Add Items Cart Reducer不工作
EN

Stack Overflow用户
提问于 2020-07-12 21:25:36
回答 1查看 247关注 0票数 0

当前正在使用react和redux将项目添加到购物车,但添加项目不起作用

我从我的收藏页面获取项目,然后将密钥传递到产品预览页面

我使用的是react-redux cartReducer,这三个文件是

就是想不出怎么才能通过鱼产品

产品页面

购物车操作

cart减速机

代码语言:javascript
复制
import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import firebase from '../../firebase/firebase';
import { connect } from 'react-redux';
import { addItem } from '../../redux/cart/cart-actions'


class FishPage extends Component {
     constructor(props) {
          super(props);
          this.ref = firebase.firestore().collection('fishproducts');
          this.unsubscribe = null;
          this.state = {
            fishproducts: []
          };
        }

     componentDidMount() {
          const ref = firebase.firestore().collection('fishproducts').doc(this.props.match.params.id);
          ref.get().then((doc) => {
               if (doc.exists) {
                    this.setState({
                         fishproducts: doc.data(),
                         key: doc.id,
                         isLoading: false
                    });
               } else {
                    console.log("No such document!");
               }
          });

          
     }


     render() {
          
          return (
               <div >
                    <div>
                         <div>
                              <h4><Link to="/">back</Link></h4>
                              <h3>
                                   {this.state.fishproducts.name}
                              </h3>
                         </div>
                         <div >
                              <dl>
                                   <dt>Description:</dt>
                                   <dd>{this.state.fishproducts.description}</dd>
                                   <dt>Discount:</dt>
                                   <dd>{this.state.fishproducts.discount}</dd>
                                   <dt>Size:</dt>
                                   <dd>{this.state.fishproducts.size}</dd>
                                   <dt>Weight:</dt>
                                   <dd>{this.state.fishproducts.weight}</dd>
                                   <dt>Price:</dt>
                                   <dd>{this.state.fishproducts.price}</dd>
                                   <dt>Stock:</dt>
                                   <dd>{this.state.fishproducts.stock}</dd>
                              </dl>
                              <button onClick={() => addItem(this.state.fishproducts)} >ADD TO CART</button>
                         </div>
                    </div>
               </div>
          );
     }
}

const mapDispatchToProps = dispatch => ({
     addItem: item => dispatch(addItem(item))
})

export default connect(null, mapDispatchToProps)(FishPage);```




this is cart action page 



```import CartActionTypes from './cart-types';

导出常量toggleCartHidden = () => ({

type:CartActionTypes.TOGGLE_CART_HIDDEN

});

导出常量项目=项目=> ({

代码语言:javascript
复制
type: CartActionTypes.ADD_ITEM,
代码语言:javascript
复制
payload: item

})`

这是小车减速机

代码语言:javascript
复制
const INITIAL_STATE = {
    hidden: true,
    cartItems: []
  };

  export const cartReducer = (state = INITIAL_STATE, action) => {
    switch (action.type) {
      case CartActionTypes.TOGGLE_CART_HIDDEN:
        return {
          ...state,
          hidden: !state.hidden
        };
        case CartActionTypes.ADD_ITEM:
          return {
            ...state,
            //cartItems: addItem(state.cartItems, action.payload)
            cartItems: [...state.cartItems,action.payload]
          };
    
        default:
            return state;
    }
  }

  export default cartReducer;```





cant figure out how to pass fishproducts
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-21 16:06:16

所以React的概念是你需要通过一个函数来访问Firebase。为此,您应该使用功能组件。React允许Hooks在没有构造函数的情况下访问你的状态,所以这就是全部,然后你需要使用分派。

代码语言:javascript
复制
import React, { useState, useEffect } from 'react';
import firebase from '../../firebase/firebase';
import { Link } from 'react-router-dom';
import { connect , useDispatch} from "react-redux";
import { addItem}  from '../../redux/cart/cart-actions';



const FishPage = (props) => {

        
    const [state, setState] = useState({
        name: '',
        … rest of the values
        isLoading: true,
    })
    const { name, … rest of the values } = state;

    useEffect(() => {
        setState({ isLoading: true });
        const ref = firebase.firestore().collection('fishproducts').doc(props.match.params.id);
        ref.get().then((doc) => {
            setState({
                name: doc.data().name,
                … rest of the values
        
                isLoading: false,
            });
        })
    }, [props.match.params.id])


    const  item = [];
    const dispatch = useDispatch();

    return (
    
        <div >
                    <div>
                        //your body here
                              <button onClick={() => dispatch(addItem(item))} >ADD TO CART</button>
                         </div>
                    </div>
               </div>
    );

}

const mapDispatchToProps = dispatch => {
    return{
        addItem: (item) => dispatch(addItem(item))
    }
}

export default connect(null, mapDispatchToProps)(FishPage)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62861669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档