2017-05-01 10 views
0

深くネストされたコンポーネントからディスパッチを呼び出そうとしています。 私はReduxとReactを使用しています。ReduxとReact。 Uncaught TypeError:未定義のプロパティ 'dispatch'を読み取ることができません

マイアプリの構造は次のとおりです。

[アプリケーション]

---- [メイン]

-------- [ShopCoffee]

---- ---- [その他のコンポーネント]

私はapp.jsxから 'dispatch'を呼び出すと動作します。

メインコンポーネントから 'dispatch'を呼び出すと、render()メソッドからのディスパッチ(警告はありますが動作します)を呼び出すと動作しますが、他の場所から 'ディスパッチ'を呼び出すと機能しません。

は私が小道具としてプロバイダとaddNewItemToCart()を使用して店を渡すが、私はShopCoffeeコンポーネントからaddNewItemToCartを呼び出すときに、エラーがある: 「キャッチされない例外TypeError:未定義のプロパティを読み取ることができません 『派遣』」ここで

App.jsx

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

 
import Main from "./components/main.component.jsx"; 
 
import { createStore } from "redux"; 
 
import { Provider } from "react-redux"; 
 

 
var app = document.getElementById("app"); 
 

 
function mainAppReducer(state, action) { 
 
\t if (!state) return { 
 
\t \t text: 'test text 1231' 
 
\t } 
 
\t switch (action.type) { 
 
\t \t case 'ADD_TO_CART' : console.log('ADD_TO_CART'); 
 
\t \t return Object.assign({}, state, { text : action.text }); 
 
\t } 
 
} 
 
const store = createStore(mainAppReducer); 
 

 
store.dispatch ({ 
 
\t type : 'ADD_TO_CART', 
 
\t text : 'One more message! ;)' 
 
}); 
 

 
store.dispatch ({ 
 
\t type : 'ADD_TO_CART', 
 
\t text : 'Text text message from redux! ;)' 
 
}); 
 

 
console.log("store.getState() == ", store.getState()); 
 

 
var render =() => ReactDOM.render(<Provider store={store}><Main /></Provider>, app); 
 
store.subscribe(render); 
 
render(); 
 

 
console.log("store.getState() == ", store.getState());

:私のプログラムであります

main.component.jsx

import React from "react"; 
 
import Header from "./header.component.jsx"; 
 
import Footer from "./footer.component.jsx"; 
 
import Cart from "./cart.component.jsx"; 
 
import Checkout from "./checkout.component.jsx"; 
 
import ShopCoffee from "./shop-coffee.component.jsx"; 
 
import Rent from "./rent.component.jsx"; 
 
import Repair from "./repair.component.jsx"; 
 
import Contacts from "./contacts.component.jsx"; 
 
import { connect } from "react-redux"; 
 

 
export class Main extends React.Component { 
 
\t constructor(props) { 
 
\t \t super(props); 
 
\t } 
 
\t addNewItemToCart(itemsInCart) { 
 
\t \t console.log("works"); 
 
\t \t console.log("addNewItemToCart() :: itemData == ", itemsInCart); \t \t 
 
\t \t console.log("from Main: this.props", this.props); 
 
\t \t this.props.dispatch({type : 'ADD_TO_CART'}); 
 
\t 
 
\t } 
 

 
\t getItemsInCart(itemsInCart) { 
 
\t \t console.log("getItemsInCart() :: itemData == ", itemsInCart); 
 
\t \t return itemsInCart; 
 
\t } 
 

 
\t render() { 
 
\t \t console.log('(from render) this.props == ', this.props); 
 
\t \t console.log('dispatch==', this.props.dispatch); 
 
\t \t 
 
\t \t return (
 
\t \t \t <main> 
 
\t \t \t \t <Header /> 
 
\t \t \t \t <Cart getItemsInCart = {this.getItemsInCart} /> 
 
\t \t \t \t <Checkout /> 
 
\t \t \t \t <ShopCoffee addNewItemToCart = {this.addNewItemToCart}/> 
 
\t \t \t \t <Rent /> 
 
\t \t \t \t <Repair /> 
 
\t \t \t \t <Contacts /> \t \t \t \t 
 
\t \t \t \t <Footer /> 
 
\t \t \t </main> 
 
\t \t); 
 
\t } 
 
} 
 

 
export default connect((store) => store)(Main);

ショップcoffee.component.jsx

import React from "react"; 
 

 
export default class ShopCoffee extends React.Component { 
 
\t constructor(props) { 
 
\t \t super(props); 
 
\t \t console.log("ShopCoffee /constructor()"); 
 
\t \t console.log("ShopCoffee/this.props", this.props); 
 
\t \t this.state = {shopItems : [], itemsInCart : []}; 
 
\t } 
 

 
// =============== Добавляет выбранный товар в корзину =============== 
 
\t addItemToCart(i) { 
 
\t \t console.log("addItemToCart() i == ",i); 
 
\t \t console.log("addItemToCart() :: this.props", this.props); 
 
\t \t let newSelectedItem = this.state.shopItems[i]; 
 
\t \t this.state.itemsInCart.push(newSelectedItem); 
 
\t \t this.props.addNewItemToCart(this.state.itemsInCart); 
 

 

 
\t } 
 

 
\t getData() { 
 
\t \t const url="/data/coffee.json"; 
 
\t \t fetch(url) 
 
\t \t \t .then((resp) => resp.json()) 
 
\t \t \t .then((data) => { 
 
\t \t \t \t this.state.shopItems = data; 
 
\t \t \t }) 
 
\t \t .catch((error) => console.error("Ошибка загрузки данных из файла", url)); 
 
\t \t 
 
\t } 
 

 

 
\t componentWillMount() { 
 
\t \t this.getData(); 
 
\t } 
 
\t componentDidMount() { 
 
     setInterval(() => this.setState(this.state), 1000); 
 
    } 
 

 
\t render() { 
 
\t \t var itemsArr = []; 
 
\t \t var itemsRow = []; 
 

 
\t \t //console.log("this.state.shopItems ===", this.state.shopItems); 
 
\t \t for (let i=0; i < this.state.shopItems.length; i++) { 
 

 
\t \t \t let item = this.state.shopItems[i]; 
 

 
\t \t \t 
 
\t \t \t itemsArr.push(
 
\t \t \t \t \t \t \t <div className="shop-coffee__item" key={"item"+i}> 
 
\t \t \t \t \t \t \t \t <div className="shop-coffee__item__inner"> 
 
\t \t \t \t \t \t \t \t \t <div className="row"> 
 
\t \t \t \t \t \t \t \t \t \t <div className="shop-coffee__item__kg"> 
 
\t \t \t \t \t \t \t \t \t \t \t <p className="shop-coffee__item__kg__text">{item.weight}</p> 
 
\t \t \t \t \t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t \t \t \t \t <div className="shop-coffee__item__space"> 
 
\t \t \t \t \t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t \t \t \t \t <div className="shop-coffee__item__price"> 
 
\t \t \t \t \t \t \t \t \t \t \t <p className="shop-coffee__item__price__text">{item.price}</p> 
 
\t \t \t \t \t \t \t \t \t \t </div> \t 
 
\t \t \t \t \t \t \t \t \t </div> \t 
 
\t \t \t \t \t \t \t \t \t <div className="row"> \t \t \t \t \t \t \t \t \t \t \t \t 
 
\t \t \t \t \t \t \t \t \t \t <div className="shop-coffee__item__image"> 
 
\t \t \t \t \t \t \t \t \t \t \t <img className="shop-coffee__item__image__img" src="img/template-img__coffee-shop.jpg" /> 
 
\t \t \t \t \t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t \t \t \t <div className="row"> 
 
\t \t \t \t \t \t \t \t \t \t <div className="shop-coffee__item__description"> 
 
\t \t \t \t \t \t \t \t \t \t \t <p className="shop-coffee__item__description__text"> 
 
\t \t \t \t \t \t \t \t \t \t \t \t {item.title} 
 
\t \t \t \t \t \t \t \t \t \t \t </p> 
 
\t \t \t \t \t \t \t \t \t \t \t <p className="shop-coffee__item__description__text"> 
 
\t \t \t \t \t \t \t \t \t \t \t \t {item.description} 
 
\t \t \t \t \t \t \t \t \t \t \t </p> 
 
\t \t \t \t \t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t \t \t \t <div className="row"> 
 
\t \t \t \t \t \t \t \t \t \t <div className="shop-coffee__item__button-container"> 
 
\t \t \t \t \t \t \t \t \t \t \t <button className="shop-coffee__item__button-container__button" onClick={this.addItemToCart.bind(this, i)}> 
 
\t \t \t \t \t \t \t \t \t \t \t \t Заказать 
 
\t \t \t \t \t \t \t \t \t \t \t </button> 
 
\t \t \t \t \t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t \t </div> 
 
\t \t \t); 
 
\t \t \t 
 
\t \t \t if (((i>0) && (i%3 == 0)) || (i == this.state.shopItems.length-1)) { 
 
\t \t \t \t itemsRow.push(
 
\t \t \t \t \t \t \t <div className="row" key={"row"+i}> 
 
\t \t \t \t \t \t \t \t {itemsArr} 
 
\t \t \t \t \t \t \t </div>); 
 
\t \t \t \t itemsArr = []; 
 

 
\t \t \t } 
 

 
\t \t } 
 
\t \t return (
 
\t \t \t \t <section className="shop-coffee"> 
 
\t \t \t \t \t <div className="container"> 
 
\t \t \t \t \t \t <div className="row"> 
 
\t \t \t \t \t \t \t <div className="shop-coffee__title-container"> 
 
\t \t \t \t \t \t \t \t <h2 className="shop-coffee__title-container__title"> 
 
\t \t \t \t \t \t \t \t \t Магазин кофе 
 
\t \t \t \t \t \t \t \t </h2> 
 
\t \t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t </div> 
 

 
\t \t \t \t 
 
\t \t \t \t \t \t \t {itemsRow} 
 

 
\t \t \t \t \t \t 
 
\t \t \t \t \t </div> 
 
\t \t \t \t </section> 
 
\t \t); 
 
\t } 
 
}

答えて

1

私はあなたに結合コールバックを欠けていると思いますあなたのコンポーネントのコンストラクタのクラスインスタンスそうKE:

constructor(props) { 
    super(props); 
    ... 
    this.addNewItemToCart = this.addNewItemToCart.bind(this); 
} 

は、基本的には:あなたがES6クラスを使用する場合は、詳細はドキュメントを反応させるのを参照してください コールバック関数をバインドする必要があります。

https://facebook.github.io/react/docs/handling-events.html

+0

は、それが助けました!ありがとうございました! – alexfrize

関連する問題