Created
May 14, 2017 09:42
-
-
Save j0nas/d597b3e7f6a6718f9c7c8ea0734d8c47 to your computer and use it in GitHub Desktop.
Example of simple form made with vanialla React/Redux
This file contains 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
const UPDATE_INPUT_VALUE = 'UPDATE_INPUT_VALUE'; | |
export const changeField = (name, value) => ({ | |
type: UPDATE_INPUT_VALUE, | |
name, | |
value, | |
}); | |
const defaultState = { | |
firstname: 'test', | |
lastname: 'test', | |
} | |
export default (state = defaultState, action) => { | |
switch (action.type) { | |
case UPDATE_INPUT_VALUE: | |
return {...state, [action.name]: action.value} | |
default: | |
return state; | |
} | |
}; |
This file contains 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 from 'react'; | |
import PropTypes from 'prop-types'; | |
const FormComponent = ({onChange, firstName, lastName}) => | |
<div> | |
<input name="firstname" type="text" onChange={onChange} value={firstName} /> | |
<input name="lastname" type="text" onChange={onChange} value={lastName} /> | |
</div>; | |
FormComponent.propTypes = { | |
onChange: PropTypes.func.isRequired, | |
firstName: PropTypes.string.isRequired, | |
lastName: PropTypes.string.isRequired, | |
}; | |
export default FormComponent; |
This file contains 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 {connect} from "react-redux"; | |
import {changeField} from "../ducks"; | |
import FormComponent from "../FormComponent"; | |
export defualt connect( | |
state => ({ | |
firstname: state.customer.firstname, | |
lastname: state.customer.lastname, | |
}), | |
dispatch => ({ | |
onChange: event => dispatch(event.target.name, event.target.value), | |
}), | |
)(FormComponent); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment