Created
September 20, 2017 12:34
-
-
Save elcodabra/67552688c4d15bcdeeceb9697519fcbd to your computer and use it in GitHub Desktop.
Adding Validation for React
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, Header | |
import store from './store'; | |
import { Provider } from 'react-redux'; | |
const App = () => { | |
return ( | |
<Provider store={store}> | |
<div className="container"> | |
<div className="starter-template"> | |
<Header /> | |
{/* ... */} | |
</div> | |
</div> | |
</Provider> | |
) | |
} | |
export default App; |
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 BackendService from '../../../services/backend'; | |
import { Field, reduxForm } from 'redux-form'; | |
import classnames from 'classnames'; | |
export const required = value => value ? undefined : 'Field is required'; | |
const renderField = ({ | |
input, | |
placeholder, | |
type, | |
meta: {touched, error, warning} | |
}) => ( | |
<div> | |
<textarea | |
className={classnames("form-control", {"is-invalid": touched && error})} | |
{...input} type={type} placeholder={placeholder}> | |
</textarea> | |
{touched && error && | |
<div className="invalid-feedback"> | |
{error} | |
</div> | |
} | |
</div> | |
) | |
class CreateTweet extends React.Component { | |
render() { | |
const {handleSubmit, pristine, reset, submitting, valid} = this.props; | |
return ( | |
<form onSubmit={handleSubmit}> | |
<div className="form-group"> | |
<Field | |
type="text" | |
name="content" | |
placeholder={this.props.placeholder} | |
component={renderField} | |
validate={[required]} | |
/> | |
</div> | |
<button | |
type="submit" | |
className={classnames("btn","btn-primary")} | |
disabled={!valid}> | |
Submit | |
</button> | |
</form> | |
) | |
} | |
} | |
export default reduxForm({form: 'tweet'})(CreateTweet); |
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 { combineReducers } from 'redux'; | |
import { reducer as formReducer } from 'redux-form'; | |
const rootReducer = combineReducers({ | |
form: formReducer | |
}); | |
export default rootReducer; |
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 { createStore, applyMiddleware, compose } from 'redux'; | |
import thunk from 'redux-thunk'; | |
import { createLogger } from 'redux-logger'; | |
import reducer from './reducer'; | |
const middleware = [ thunk ]; | |
let composeEnhancers = (v) => (v); | |
if (process.env.NODE_ENV !== 'production') { | |
middleware.push(createLogger()) | |
composeEnhancers = | |
typeof window === 'object' && | |
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ? | |
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({ | |
// Specify extension’s options like name, actionsBlacklist, actionsCreators, serialize... | |
}) : compose; | |
} | |
const store = createStore( | |
reducer, | |
composeEnhancers( | |
applyMiddleware(...middleware) | |
) | |
); | |
export default store; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment