2017-02-20 45 views
0

我使用Redux将数据保存在全局状态,该状态基于用户的操作进行更新。当用户点击一个按钮时,它应该更新状态,并将当前状态显示在按钮上。相反,最初的遗迹显示不变。我已经列出了我的组件和reducer。Redux商店没有使用React进行更新

主要成分:

import React, { Component, PropTypes } from 'react'; 
import Picture from '../../components/picture.jsx'; 
import ShoppingCart from '../../components/shoppingcart.jsx'; 
import { browserHistory } from 'react-router'; 
import { createStore } from 'redux'; 
import cart from '../../reducers/index.js'; 
var flag = false; 
const store = createStore(cart); 


export default class Products extends Component { 

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

    componentWillMount() { 
     fetch('/t') 
     .then((result) => { 
     return result.json(); 
     }) 
     .then((re) => { 
     flag = true; 
     this.setState({ clothingData: re }); 
     }) 
     .catch(function(error){ 
     console.log(error); 
     }); 
    } 

    componentDidMount(){ 
     fetch('/ship') 
     .then((result) => { 
     return result.json(); 
     }) 
     .then((res) => { 
     console.log(res); 
     }) 
     .catch(function(error){ 
     console.log(error); 
     }); 
    } 

    render(){ 
    if (!flag){ 
    return (
     <div> 
     </div> 
    ); 
    } 
    else{ 
     //console.log(this.state.clothingData[0].path); 
     //console.log(this.state.clothingData[0].fileName); 
     //console.log(this.state.clothingData); 
     return (
     <div> 
     <Picture src = {this.state.clothingData} onClick = {() => {browserHistory.push('/Product'); }} name = {"joe"} /> 
     <ShoppingCart value={ store.getState() } onIncrement={() => store.dispatch({ type: 'INCREMENT' })} /> 
     </div> 
    ); 
    } 
} 

} 

购物车组件:

import React, { Component, PropTypes } from 'react'; 
export default class ShoppingCart extends Component { 
    render(){ 
     const { onIncrement } = this.props 
    return(
     <div> 
     <button onClick = {onIncrement}> {this.props.value} </button> 
     </div> 
    ); 
    } 
} 

减速机:

export default (state = 2, action) => { 
    switch (action.type) { 
    case 'INCREMENT': 
     return state + 1 
    case 'DECREMENT': 
     return state - 1 
    default: 
     return state 
    } 
} 

回答

1

由于您使用的终极版,你有没有考虑使用的react-redux绑定反应整合呢?

通过商店触发行为不被认为是最佳做法,如您在此comment中所见。

查看Provider组件。