Skip to content

Instantly share code, notes, and snippets.

@inoh
Last active November 18, 2017 22:35
Show Gist options
  • Save inoh/8fe703f109ba791304c95cd7bb722ae1 to your computer and use it in GitHub Desktop.
Save inoh/8fe703f109ba791304c95cd7bb722ae1 to your computer and use it in GitHub Desktop.
import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux'
import Hello from '../components/Hello';
class App extends Component {
static propTypes = {
dispatch: PropTypes.func.isRequired,
hellos: PropTypes.arrayOf(PropTypes.shape({
value: PropTypes.string.isRequired,
note: PropTypes.string.isRequired,
})).isRequired,
}
onClick = () => {
const { dispatch } = this.props;
// Helloを追加するactionをdispatch(reducer経由でStoreに伝達)する
dispatch({
type: 'ADD_HELLO',
payload: {
value: 'add',
note: '追加したよ',
},
});
}
render() {
const { hellos } = this.props;
return (
<div>
{hellos.map(hello => (
<Hello value={hello.value}>
<p>{hello.note}</p>
</Hello>
))}
<br />
<button onClick={this.onClick}>Helloを追加</button>
</div>
);
}
}
const mapStateToProps = (state) => ({
hellos: state.hellos,
});
export default connect(mapStateToProps)(App);
@inoh
Copy link
Author

inoh commented Nov 18, 2017

初期

image

追加後

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment