Skip to content

Instantly share code, notes, and snippets.

@madan712
Created June 1, 2019 10:32
Show Gist options
  • Save madan712/166355721aa8adb34672d8da526ef831 to your computer and use it in GitHub Desktop.
Save madan712/166355721aa8adb34672d8da526ef831 to your computer and use it in GitHub Desktop.
App.js - Example with React, Redux and Axios API
import React, { Component } from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import * as appAction from './AppAction';
class App extends Component {
constructor(props, context) {
super(props, context);
this.submit = this.submit.bind(this);
this.updateName = this.updateName.bind(this);
}
submit(event) {
event.preventDefault();
this.props.appAction.callSayHello(this.props.name);
}
updateName(event) {
this.props.appAction.updateName(event.target.value);
}
render() {
return (
<div>
<form onSubmit={this.submit}>
<label>
Name: <input type="text" id="name" value={this.props.name} onChange={this.updateName} />
</label>
<button type="submit">submit</button>
</form>
<div>Response : {this.props.response}</div>
</div>
);
}
}
function mapStateToProps(state) {
return {
name: state.name,
response: state.response
};
}
function mapDispatchToProps(dispatch) {
return {
appAction: bindActionCreators(appAction, dispatch)
};
}
export default connect(mapStateToProps, mapDispatchToProps)(App);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment