Created
April 11, 2016 16:02
-
-
Save namelos/34c042c7a6f88808088b17de258a34c4 to your computer and use it in GitHub Desktop.
react native with redux and redux form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { | |
AppRegistry, Component, StyleSheet, | |
Text, View, TouchableOpacity, TextInput | |
} from 'react-native' | |
import { createStore, combineReducers, bindActionCreators } from 'redux' | |
import { connect, Provider } from 'react-redux' | |
import { reducer as form, reduxForm } from 'redux-form' | |
const Button = ({ children, onPress }) => <TouchableOpacity onPress={onPress}> | |
<Text>{children}</Text> | |
</TouchableOpacity> | |
const counter = (state = 0, action) => { | |
switch (action.type) { | |
case 'INCREMENT': return state + 1 | |
case 'DECREMENT': return state - 1 | |
default: return state | |
} | |
} | |
const Counter = connect( | |
({ counter }) => ({ counter }), | |
dispatch => bindActionCreators({ | |
increment: () => ({ type: 'INCREMENT' }), | |
decrement: () => ({ type: 'DECREMENT' })}, | |
dispatch) | |
)(({ counter, increment, decrement }) => <View> | |
<Text>Counter: {counter}</Text> | |
<Button onPress={increment}>Increment</Button> | |
<Button onPress={decrement}>Decrement</Button> | |
</View>) | |
const Input = ({value, onChange}) => <TextInput | |
style={{height: 30, borderColor: 'gray', borderWidth: 1}} | |
value={value} | |
onChangeText={onChange} | |
/> | |
const Form = reduxForm({ | |
form: 'simple', | |
fields: ['firstName', 'lastName'] | |
})(({ fields: { firstName, lastName }, handleSubmit }) => <View> | |
<Input {...firstName}/> | |
<Text>{firstName.value}</Text> | |
<Input {...lastName}/> | |
<Text>{lastName.value}</Text> | |
</View>) | |
const store = createStore(combineReducers({ counter, form })) | |
const App = () => <Provider store={store}> | |
<View style={{ marginTop: 20 }}> | |
<Counter /> | |
<Form /> | |
</View> | |
</Provider> | |
AppRegistry.registerComponent('App', () => App) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment