Skip to content

Commit 1b02059

Browse files
committed
Add UpdateBackground feature
1 parent 6ab28d0 commit 1b02059

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

src/actions/index.jsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
export const updateBackgroundColor = (hex) => ({
3+
type: "UPDATE_BACKGROUND_COLOR",
4+
hex
5+
})

src/containers/Controller.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,35 @@ import React from 'react';
22
import { connect } from 'react-redux';
33
import creater from '../components/creater';
44
import { SketchPicker } from 'react-color';
5+
import { updateBackgroundColor } from '../actions';
56

6-
const handleChangeComplete = (color) => {
7-
console.log(color.hex);
8-
return {
9-
background: color.hex
10-
};
11-
}
7+
// const handleChangeComplete = (color) => {
8+
// console.log(color.hex);
9+
// dispatch(updateBackgroundColor(color.hex));
10+
// return {
11+
// background: color.hex
12+
// };
13+
// }
1214

13-
const Controller = () => {
15+
const Controller = ({ updateBackgroundColor }) => {
1416
let backgroundColor = 'red';
1517
return(
1618
<div>
1719
Controller
1820
<SketchPicker
1921
color={backgroundColor}
20-
onChangeComplete={handleChangeComplete}
22+
onChangeComplete={(color) => updateBackgroundColor(color.hex)}
2123
/>
2224
<button onClick={() => creater('my-node')}>CREATE</button>
23-
24-
2525
</div>
2626
)
2727
}
28-
const mapStateToProps = (state) => state
29-
const mapDispatchToProps = (dispatch) => ({});
28+
29+
const mapStateToProps = (state) => ({ backgroundColor: state.backgroundColor });
30+
31+
const mapDispatchToProps = (dispatch) => ({
32+
updateBackgroundColor: (hex) => dispatch(updateBackgroundColor(hex))
33+
});
3034

3135
export default connect(
3236
mapStateToProps,

src/reducers/background.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const updateBackgroundColor = (state = [], action) => {
2+
switch(action.type){
3+
case "UPDATE_BACKGROUND_COLOR":
4+
return action.hex;
5+
default:
6+
return state;
7+
}
8+
}
9+

src/reducers/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { combineReducers } from 'redux';
2+
import { updateBackgroundColor } from './background';
23

3-
export default combineReducers({}
4-
//
5-
);
4+
export default combineReducers({
5+
updateBackgroundColor
6+
});

0 commit comments

Comments
 (0)