gpt4 book ai didi

javascript - React Redux 存储将 props 传递给 child 的问题

转载 作者:行者123 更新时间:2023-11-28 14:59:27 25 4
gpt4 key购买 nike

我想要完成的是拥有一个“ Controller ”文件,将所有组件渲染到我的页面。我尝试实现 redux 将 Prop 传递给我的子组件,但我没有让它工作。这是我的文件:

index.jsx

import React from 'react';
import ReactDOM from 'react-dom';
import {Provider} from 'react-redux'
import store from './js/store.jsx'
import App from './js/App.jsx'

// Assigning the sections
const nav = document.getElementsByClassName('cart')[0];

//Render the components
ReactDOM.render(<Provider store={store}>
<App />

</Provider>, nav)

App.jsx

import React from 'react'
import Cart from './components/cart/Cart.jsx'

export default class App extends React.Component {
constructor(props) {
super(props);
}

render() {
return <Cart />
}
}

购物车.jsx

import React from 'react'
import { connect } from 'react-redux'
import {fetchCart} from '../../actions/cartActions'
var ReactDOM = require('react-dom');


@connect((store) => {

})
export default class Cart extends React.Component {
componentWillMount() {
this.props.dispatch(fetchCart())
}
constructor(props) {
super(props);
this.state = {

};

}

render() {
return( <div>

<div className="cart-scroll-area">
{/* Bryt ut till egna komponenter */}
<header className="cart-header"></header>
<header className="cart-meta">
<div className="cart-meta-info">
<div className="cart-timer">Avsluta din order inom<br /><strong>xxxx</strong><br /></div>
<div className="status ok">
för leverans tidigast xxxx
</div>
</div>
</header>
<section className="cart-section cart-errors"></section>
<section className="cart-section cart-section-homelist"></section>
<section className="cart-section cart-section-bag-header"></section>
<section className="cart-section cart-section-prenumeration"></section>
<section className="cart-section cart-section-flexbag"></section>
<section className="cart-section cart-section-recipes"></section>
<section className="cart-section cart-section-mostbought"></section>
<section className="cart-section cart-section-accessories"></section>
</div>
<div className="cart-scroll-footer">
<div className="cart-total"></div>
<div className="cart-total-price"></div>
<footer className="cart-footer"></footer>
</div>
</div>
)
}
}

-

cartActions.js

let restPath = "/ShoppingCart";

export function fetchCart(){
return {
type: "FETCH_CART",
payload: {data:["Event 1", "Event 2"]}
//payload: axios.get("http://localhost:3000/api/v1/events")
}
}

cartReducer.js

const initialState = {
fetching: false,
fetched: false,
cart: [],
error: null
}

export default function reducer(state=initialState, action=null) {
console.log(action.payload)
switch (action.type){

case "FETCH_CART_PENDING" : {
return {...state, fetching:true}
break;
}
case "FETCH_CART_REJECTED" : {
return {...state, fetching:false, error:action.payload}
break;
}
case "FETCH_CART_FULFILLED" : {
return {...state,
fetching:false,
fetched:true,
cart:action.payload.data
}
break;
}
case "FETCH_CART" : {
return {...state,
fetching:false,
fetched:true,
cart:action.payload.data
}
break;
}
}
return state
}

最后这是我的 store.jsx

import {applyMiddleware, createStore} from 'redux'
import logger from 'redux-logger'
import thunk from 'redux-thunk'
import promise from 'redux-promise-middleware'
import reducer from './reducers'

const middleware = applyMiddleware(promise(), thunk, logger())

export default createStore(reducer, middleware)

现在我得到 this.props.dispatch 不是一个函数。而且我没有得到任何包含 "payload: {data:["Event 1", "Event 2"]}"

谁能帮帮我吗? :(

最佳答案

您对 connect 的使用不正确。您希望将状态和调度程序映射到组件的 props。此外,我建议连接顶级 App 容器并将 props 传递给其子级。尝试这样的事情:

App.jsx

import React from 'react'
import Cart from './components/cart/Cart.jsx'
import {connect} from 'react-redux'
import {fetchCart} from '../../actions/cartActions'

class App extends React.Component {
constructor(props) {
super(props);
}

render() {
return <Cart
fetching={this.props.fetching}
fetched={this.props.fetched}
cart={this.props.cart}
error={this.props.error}
fetchCart={this.props.fetchCart} />
}
}

const mapStateToProps = (state) => {
// These will be passed as props into the component.
return {
fetching: state.fetching,
fetched: state.fetched,
cart: state.cart,
error: state.error
};
};

const mapDispatchToProps = (dispatch) => {
// These will be passed as props into the component.
return {
fetchCart: () => dispatch(fetchCart())
};
};

export default connect(mapStateToProps, mapDispatchToProps)(App);

Cart.jsx

import React from 'react'
import ReactDOM from 'react-dom';

export default class Cart extends React.Component {
componentWillMount() {
this.props.fetchCart();
}

constructor(props) {
super(props);
this.state = { };
}

render() {
// this.props.fetching, this.props.fetched etc. should exist.
console.log(this.props);

return(
<div>
<div className="cart-scroll-area">
{/* Bryt ut till egna komponenter */}
<header className="cart-header"></header>
<header className="cart-meta">
<div className="cart-meta-info">
<div className="cart-timer">Avsluta din order inom<br /><strong>xxxx</strong><br /></div>
<div className="status ok">
för leverans tidigast xxxx
</div>
</div>
</header>
<section className="cart-section cart-errors"></section>
<section className="cart-section cart-section-homelist"></section>
<section className="cart-section cart-section-bag-header"></section>
<section className="cart-section cart-section-prenumeration"></section>
<section className="cart-section cart-section-flexbag"></section>
<section className="cart-section cart-section-recipes"></section>
<section className="cart-section cart-section-mostbought"></section>
<section className="cart-section cart-section-accessories"></section>
</div>
<div className="cart-scroll-footer">
<div className="cart-total"></div>
<div className="cart-total-price"></div>
<footer className="cart-footer"></footer>
</div>
</div>
)
}
}

关于javascript - React Redux 存储将 props 传递给 child 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41674591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com