Skip to content

Instantly share code, notes, and snippets.

@j0nas
Created May 14, 2017 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j0nas/d597b3e7f6a6718f9c7c8ea0734d8c47 to your computer and use it in GitHub Desktop.
Save j0nas/d597b3e7f6a6718f9c7c8ea0734d8c47 to your computer and use it in GitHub Desktop.
Example of simple form made with vanialla React/Redux
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;
}
};
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;
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