Initial commit from boilerplate
This commit is contained in:
2
app/actions/actionTypes.js
Normal file
2
app/actions/actionTypes.js
Normal file
@@ -0,0 +1,2 @@
|
||||
export const INCREMENT = 'INCREMENT';
|
||||
export const DECREMENT = 'DECREMENT';
|
13
app/actions/counterActions.js
Normal file
13
app/actions/counterActions.js
Normal file
@@ -0,0 +1,13 @@
|
||||
import * as types from './actionTypes';
|
||||
|
||||
export function increment() {
|
||||
return {
|
||||
type: types.INCREMENT
|
||||
};
|
||||
}
|
||||
|
||||
export function decrement() {
|
||||
return {
|
||||
type: types.DECREMENT
|
||||
};
|
||||
}
|
36
app/components/counter.js
Normal file
36
app/components/counter.js
Normal file
@@ -0,0 +1,36 @@
|
||||
import React, {Component} from 'react';
|
||||
import {StyleSheet, View, Text, TouchableOpacity} from 'react-native';
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
button: {
|
||||
width: 100,
|
||||
height: 30,
|
||||
padding: 10,
|
||||
backgroundColor: 'lightgray',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
margin: 3
|
||||
}
|
||||
});
|
||||
|
||||
export default class Counter extends Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
}
|
||||
|
||||
render() {
|
||||
const { counter, increment, decrement } = this.props;
|
||||
|
||||
return (
|
||||
<View style={{flex: 1, alignItems: 'center', justifyContent: 'center' }}>
|
||||
<Text>{counter}</Text>
|
||||
<TouchableOpacity onPress={increment} style={styles.button}>
|
||||
<Text>up</Text>
|
||||
</TouchableOpacity>
|
||||
<TouchableOpacity onPress={decrement} style={styles.button}>
|
||||
<Text>down</Text>
|
||||
</TouchableOpacity>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
}
|
21
app/containers/app.js
Normal file
21
app/containers/app.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import React, {Component} from 'react';
|
||||
import { createStore, applyMiddleware, combineReducers } from 'redux';
|
||||
import { Provider } from 'react-redux';
|
||||
import thunk from 'redux-thunk';
|
||||
|
||||
import * as reducers from '../reducers';
|
||||
import CounterApp from './counterApp';
|
||||
|
||||
const createStoreWithMiddleware = applyMiddleware(thunk)(createStore);
|
||||
const reducer = combineReducers(reducers);
|
||||
const store = createStoreWithMiddleware(reducer);
|
||||
|
||||
export default class App extends Component {
|
||||
render() {
|
||||
return (
|
||||
<Provider store={store}>
|
||||
<CounterApp />
|
||||
</Provider>
|
||||
);
|
||||
}
|
||||
}
|
33
app/containers/counterApp.js
Normal file
33
app/containers/counterApp.js
Normal file
@@ -0,0 +1,33 @@
|
||||
'use strict';
|
||||
|
||||
import React, {Component} from 'react';
|
||||
import {bindActionCreators} from 'redux';
|
||||
import Counter from '../components/counter';
|
||||
import * as counterActions from '../actions/counterActions';
|
||||
import { connect } from 'react-redux';
|
||||
|
||||
// @connect(state => ({
|
||||
// state: state.counter
|
||||
// }))
|
||||
class CounterApp extends Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
}
|
||||
|
||||
render() {
|
||||
const { state, actions } = this.props;
|
||||
return (
|
||||
<Counter
|
||||
counter={state.count}
|
||||
{...actions} />
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default connect(state => ({
|
||||
state: state.counter
|
||||
}),
|
||||
(dispatch) => ({
|
||||
actions: bindActionCreators(counterActions, dispatch)
|
||||
})
|
||||
)(CounterApp);
|
22
app/reducers/counter.js
Normal file
22
app/reducers/counter.js
Normal file
@@ -0,0 +1,22 @@
|
||||
import * as types from '../actions/actionTypes';
|
||||
|
||||
const initialState = {
|
||||
count: 0
|
||||
};
|
||||
|
||||
export default function counter(state = initialState, action = {}) {
|
||||
switch (action.type) {
|
||||
case types.INCREMENT:
|
||||
return {
|
||||
...state,
|
||||
count: state.count + 1
|
||||
};
|
||||
case types.DECREMENT:
|
||||
return {
|
||||
...state,
|
||||
count: state.count - 1
|
||||
};
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
5
app/reducers/index.js
Normal file
5
app/reducers/index.js
Normal file
@@ -0,0 +1,5 @@
|
||||
import counter from './counter';
|
||||
|
||||
export {
|
||||
counter
|
||||
};
|
Reference in New Issue
Block a user