我使用React redux来构建一个数字的div,你可以增加或减去。
问题是,一切似乎看起来很好,但没有结果的浏览器
reducer-> index.js:
import { combineReducers } from 'redux'
const sumReducer = (sum = 0, action) => {
if (action.type === 'ADD' || action.type === 'SUB') {
let x = action.payload;
return x;
}
return sum;
}
export default combineReducers({
sum: sumReducer
});
**action-> index.js**
export const add = (sum) => {
return {
type: 'ADD',
payload: sum + 1
}
}
export const sub = (sum) => {
return {
type: 'SUB',
payload: sum - 1
}
}
我的组件是:upDownSum.js
import React from 'react'
import { connect } from 'react-redux'
import { add, sub } from '../action'
class UpDownSum extends React.Component {
render() {
console.log(this.props);
console.log("ghj");
return (
<div key="1">
<button onClick={() => sub(this.props.sum)}>-</button>
<div>{this.props.sum}</div>
<button onClick={() => add(this.props.sum)}>+</button>
</div>
)
}
}
const MapStateToProps = (state) => {
return {
sum: state.sum
}
}
export default connect(MapStateToProps, {add: add ,sub: sub})(UpDownSum);
app.jsimport './App.css';从“./upDownSum”导入UpDownSum
function App() {
return (
<UpDownSum/>
);
}
export default App;
谢谢你,谢谢!
1条答案
按热度按时间zed5wv101#
您正在将dispatchMap到 props,这意味着您需要确保使用
props
而不仅仅是导入的操作来分派操作: