Skip to content

Commit

Permalink
Replace redux mapState and mapDispatch with redux hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
steinarb committed Sep 9, 2023
1 parent f4ae0e8 commit dff7828
Showing 1 changed file with 8 additions and 23 deletions.
31 changes: 8 additions & 23 deletions src/main/frontend/src/components/Counter.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,29 @@
import React from 'react';
import { connect } from 'react-redux';
import { useSelector, useDispatch } from 'react-redux';
import {
DELTA_MODIFY,
INCREMENT_REQUEST,
DECREMENT_REQUEST,
} from '../reduxactions';


function Counter(props) {
const {delta, counter, onDeltaModify, onIncrement, onDecrement} = props;
export default function Counter() {
const delta = useSelector(state => state.delta);
const counter = useSelector(state => state.counter);
const dispatch = useDispatch();

return (
<div>
<h1>Counting high and low</h1>
<p>
Increment step:
<input id="delta" type="text" value={delta} onChange={onDeltaModify} />
<input id="delta" type="text" value={delta} onChange={e => dispatch(DELTA_MODIFY(e.target.value))} />
</p>
<p>
{counter}
<button onClick={onIncrement}>+</button>
<button onClick={onDecrement}>-</button>
<button onClick={() => dispatch(INCREMENT_REQUEST())}>+</button>
<button onClick={() => dispatch(DECREMENT_REQUEST())}>-</button>
</p>
</div>
);
}

function mapStateToProps(state) {
return {
delta: state.delta,
counter: state.counter,
};
}

function mapDispatchToProps(dispatch) {
return {
onDeltaModify: e => dispatch(DELTA_MODIFY(e.target.value)),
onIncrement: () => dispatch(INCREMENT_REQUEST()),
onDecrement: () => dispatch(DECREMENT_REQUEST()),
};
}

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

0 comments on commit dff7828

Please sign in to comment.