-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSquareScreen.js
67 lines (60 loc) · 2.13 KB
/
SquareScreen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import React, {Component,useReducer} from 'react'
import { StyleSheet, Text, View } from 'react-native';
import ColorCounter from './components/ColorCounter'
const COLOR_INCREMENT = 15;
const reducer = (state, action) => {
switch(action.type) {
case 'change_red':
return state.red + action.payload > 255 || state.red + action.payload < 0
? state
: {...state, red: state.red + action.payload};
case 'change_green':
return state.green + action.payload > 255 || state.green + action.payload < 0
? state
: {...state, green: state.green + action.payload};
case 'change_blue':
return state.blue + action.payload > 255 || state.blue + action.payload < 0
? state
: {...state, blue: state.blue + action.payload};
default:
return state;
}
}
const SquareScreen = () => {
const [state,dispatch] = useReducer(reducer, {red: 0, green: 0, blue: 0});
console.log(state);
const { red, green, blue} = state;
return (
<View>
<ColorCounter
onIncrease = {()=> dispatch({type: 'change_red', payload: COLOR_INCREMENT})}
onDecrease = {()=> dispatch({type: 'change_red', payload: -1 * COLOR_INCREMENT})}
color="Red"
/>
<ColorCounter
onIncrease = {()=> dispatch({type: 'change_blue', payload: COLOR_INCREMENT}) }
onDecrease = {()=> dispatch({type: 'change_blue', payload: -1 * COLOR_INCREMENT})}
color="Blue"/>
<ColorCounter
onIncrease = {()=> dispatch({type: 'change_green', payload: COLOR_INCREMENT})}
onDecrease = {()=> dispatch({type: 'change_green', payload: -1 * COLOR_INCREMENT})}
color="Green" />
<View
style={{
height: 150,
width: 150,
backgroundColor: `rgb(${red}, ${green},${blue})`
}}>
</View>
</View>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
export default SquareScreen