Skip to content
This repository was archived by the owner on Feb 2, 2024. It is now read-only.

Commit dceb475

Browse files
committed
Add working RN counter example
1 parent 60db499 commit dceb475

5 files changed

Lines changed: 112 additions & 16 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import React, { Component } from 'react';
2+
import {
3+
Button,
4+
Text,
5+
View
6+
} from 'react-native';
7+
8+
class Counter extends Component {
9+
incrementIfOdd = () => {
10+
if (this.props.value % 2 !== 0) {
11+
this.props.onIncrement();
12+
}
13+
}
14+
15+
incrementAsync = () => {
16+
setTimeout(this.props.onIncrement, 1000);
17+
}
18+
19+
render() {
20+
const { value, onIncrement, onDecrement } = this.props;
21+
return (
22+
<View>
23+
<Text>
24+
Clicked: {value} times
25+
</Text>
26+
<Button
27+
title='+'
28+
onPress={onIncrement}
29+
/>
30+
<Button
31+
title='-'
32+
onPress={onDecrement}
33+
/>
34+
<Button
35+
title='Increment if odd'
36+
onPress={this.incrementIfOdd}
37+
/>
38+
<Button
39+
title='Increment async'
40+
onPress={this.incrementAsync}
41+
/>
42+
</View>
43+
);
44+
}
45+
}
46+
47+
export default Counter;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React, { Component } from 'react';
2+
import { connect } from 'react-redux';
3+
import Counter from '../components/Counter';
4+
5+
class CounterContainer extends Component {
6+
render() {
7+
const { value, onIncrement, onDecrement } = this.props;
8+
return (
9+
<Counter
10+
value={value}
11+
onIncrement={onIncrement}
12+
onDecrement={onDecrement}
13+
/>
14+
);
15+
}
16+
}
17+
18+
export default connect(
19+
state => ({
20+
value: state.count
21+
}),
22+
dispatch => ({
23+
onIncrement: () => dispatch({ type: 'INCREMENT' }),
24+
onDecrement: () => dispatch({ type: 'DECREMENT' })
25+
})
26+
)(CounterContainer);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const rootReducer = (state = {
2+
count: 0
3+
}, action) => {
4+
switch (action.type) {
5+
case 'INCREMENT':
6+
return {
7+
...state,
8+
count: state.count + 1
9+
};
10+
case 'DECREMENT':
11+
return {
12+
...state,
13+
count: state.count - 1
14+
};
15+
default:
16+
return state;
17+
}
18+
};
19+
20+
export default rootReducer;

‎examples/ReactNativeExample/index.ios.js‎

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ import {
1313
View
1414
} from 'react-native';
1515
import { compose, createStore } from 'redux';
16+
import { Provider } from 'react-redux';
1617
import { persistStore, autoRehydrate } from 'redux-persist';
1718
import createEncryptor from 'redux-persist-transform-encrypt';
18-
19-
function rootReducer(state = {}, action) {
20-
return state;
21-
}
19+
import CounterContainer from './app/containers/CounterContainer';
20+
import rootReducer from './app/reducers';
2221

2322
const store = createStore(
2423
rootReducer,
@@ -42,18 +41,21 @@ persistStore(store, {
4241
export default class ReactNativeExample extends Component {
4342
render() {
4443
return (
45-
<View style={styles.container}>
46-
<Text style={styles.welcome}>
47-
Welcome to React Native!
48-
</Text>
49-
<Text style={styles.instructions}>
50-
To get started, edit index.ios.js
51-
</Text>
52-
<Text style={styles.instructions}>
53-
Press Cmd+R to reload,{'\n'}
54-
Cmd+D or shake for dev menu
55-
</Text>
56-
</View>
44+
<Provider store={store}>
45+
<View style={styles.container}>
46+
<Text style={styles.welcome}>
47+
Welcome to React Native!
48+
</Text>
49+
<Text style={styles.instructions}>
50+
To get started, edit index.ios.js
51+
</Text>
52+
<Text style={styles.instructions}>
53+
Press Cmd+R to reload,{'\n'}
54+
Cmd+D or shake for dev menu
55+
</Text>
56+
<CounterContainer />
57+
</View>
58+
</Provider>
5759
);
5860
}
5961
}

‎examples/ReactNativeExample/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"dependencies": {
1010
"react": "16.0.0-alpha.6",
1111
"react-native": "0.43.1",
12+
"react-redux": "^5.0.3",
1213
"redux": "^3.6.0",
1314
"redux-persist": "^4.6.0",
1415
"redux-persist-transform-encrypt": "^1.0.1"

0 commit comments

Comments
 (0)