Upgrade to Pro — share decks privately, control downloads, hide ads and more …

repost-oss

 repost-oss

Hiroki Zenigami

September 28, 2017
Tweet

More Decks by Hiroki Zenigami

Other Decks in Programming

Transcript

  1. ۩ମྫ w ྫɿ֎෦αʔϏε࿈ܞʢ࣮૷͕࠷΋γϯϓϧͩͬͨʣ w આ໌͠΍͍͢Α͏࣮ࡍͷίʔυΛมߋɾলུ͍ͯ͠·͢ 17 app/ controllers/ api/ └─

    services_controller.rb javascript/ actions/ └─ servicesActions.js features/ └─ Services.jsx reducers/ └─ services.js
  2. w ࿈ܞதͷ֎෦αʔϏεҰཡΛදࣔ͢Δ$POUBJOFS$PNQPOFOU
 هࣄ౤ߘ࣌ͷ௨஌ઌ4MBDLͳͲ w componentDidMount()͔ΒΞΫγϣϯΛ࣮ߦ͍ͯ͠Δ
 3FBDUͷϥΠϑαΠΫϧ 20 import * as

    servicesActions from '../actions/servicesActions' class Services extends Component { componentDidMount() { this.props.fetchServices() } } function mapDispatchToProps(dispatch) { return bindActionCreators(servicesActions, dispatch) } BQQKBWBTDSJQUGFBUVSFT4FSWJDFTKTY
  3. w 7JFX͔Βݺ͹ΕΔ"DUJPO w ࣍ͷΑ͏ͳܗͰ"1*ʹର͢ΔϦΫΤετΛఆٛ w )551ΫϥΠΞϯτ༻ͷϛυϧ΢ΣΞΛಋೖ͍ͯ͠Δ
 SFEVYBYJPTNJEEMFXBSF 22 export function

    fetchServices() { return { type: 'FETCH_SERVICES', payload: { request: { url: '/api/services.json' } } } } BQQKBWBTDSJQUBDUJPOTTFSWJDFT"DUJPOTKT
  4. w 3FEVDFS͸4UPSFͷ4UBUFͷมԽΛఆٛͨؔ͠਺ w ϦΫΤετ͕੒ޭͨ͠৔߹_SUCCESSͱ͍͏઀ඌ͕͍ࣙͭͨ
 λΠϓ͕ฦ٫͞ΕΔ
 SFEVYBYJPTNJEEMFXBSFͷ;Δ·͍ w "1*ͷϨεϙϯε͕4UBUFʹอ࣋͞ΕΔ
 ͜ͷ৔߹࿈ܞதͷ֎෦αʔϏεҰཡ 26

    function services(state = initialState, action) { switch (action.type) { case 'FETCH_SERVICES_SUCCESS': return action.payload.data // ࿈ܞதͷαʔϏεҰཡ default: return state } } BQQKBWBTDSJQUSFEVDFSTTFSWJDFTKT
  5. w 4UBUFΛ1SPQTʹϚοϓ w 4UBUF͕มԽ͢Δͱίϯϙʔωϯτʹ౉͢ύϥϝʔλ΋มԽ͢Δ
 7JFX͕ߋ৽͞ΕΔ 28 class Services extends Component

    { render() { return ( <div> <ServiceList services={this.props.services} /> </div> ) } } function mapStateToProps(state) { return { services: state.services } } BQQKBWBTDSJQUGFBUVSFT4FSWJDFTKTY
  6. w rails new͢Δͱ͖ʹ--webpack=reactΦϓγϣϯΛ͚ͭΔ
 webpacker:install΍yarn install͕૸Δ w ͜ͷ࣌఺Ͱ3FBDUͷͻͳܗ͕Ͱ͖͍ͯΔ w ඞཁʹԠͯ͡reduxͳͲͷϥΠϒϥϦΛΠϯετʔϧ͢Δ
 yarn

    addͰΠϯετʔϧ w ։ൃ࣌͸webpack-dev-serverΛ࣮ߦ͢Δ
 ίʔυมߋ࣌ʹ)PU.PEVMF3FQMBDFNFOUͯ͘͠ΕΔ 33 $ rails new foo --webpack=react $ bin/yarn add redux react-redux react-router-redux ... $ bin/webpack-dev-server
  7. w 8FCQBDLFSͰϏϧυͨ͠ϑΝΠϧΛ
 8FCQBDLFSͷjavascript_pack_tagΛͱ͓ͯ͠ಡΉ w ݟͨ໨͸͢΂ͯ+BWB4DSJQU 35 <!DOCTYPE html> <html> <head>

    <title>Repost</title> <%= stylesheet_pack_tag 'application' %> </head> <body> <%= javascript_pack_tag 'application' %> </body> </html> BQQWJFXTMBZPVUTBQQMJDBUJPOIUNMFSC