Reactjs Redux存储未使用React更新
Im使用Redux将数据保持在全局状态,并根据用户的操作进行更新。当用户单击一个按钮时,它应该更新状态,并在按钮上显示当前状态。相反,初始值将保持不变。我在下面列出了我的部件和减速器 主要组成部分:Reactjs Redux存储未使用React更新,reactjs,redux,Reactjs,Redux,Im使用Redux将数据保持在全局状态,并根据用户的操作进行更新。当用户单击一个按钮时,它应该更新状态,并在按钮上显示当前状态。相反,初始值将保持不变。我在下面列出了我的部件和减速器 主要组成部分: import React, { Component, PropTypes } from 'react'; import Picture from '../../components/picture.jsx'; import ShoppingCart from '../../components/sh
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>
);
}
}
}
开箱即用的redux与任何库都没有关系(如Angular、React等)。您必须手动将redux绑定到应用程序 以下是绑定redux以进行反应的说明
开箱即用的redux与任何库都没有关系(如Angular、React等)。您必须手动将redux绑定到应用程序 以下是绑定redux以进行反应的说明
既然您正在使用Redux,您是否考虑过使用绑定将其与React集成 通过商店触发这样的操作并不被认为是最佳实践,正如您在本文中看到的那样
查看组件。既然您正在使用Redux,您是否考虑过使用绑定将其与React集成 通过商店触发这样的操作并不被认为是最佳实践,正如您在本文中看到的那样 检查组件
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
}
}