Redux: React μ»΄ν¬λ„ŒνŠΈ νŒŒμΌμ—μ„œ store.dispatch () 호좜?

에 λ§Œλ“  2015λ…„ 10μ›” 19일  Β·  10μ½”λ©˜νŠΈ  Β·  좜처: reduxjs/redux

이 λ¬Έμ œκ°€ 이미 ν•΄κ²° 된 경우 μ‚¬κ³Όλ“œλ¦½λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ 문제의 ν‚€μ›Œλ“œ 검색은 λ„μ›€μ΄λ˜λŠ” λ‚΄μš©μ„ 찾지 λͺ»ν–ˆμŠ΅λ‹ˆλ‹€.

ꡬ성 μš”μ†Œ νŒŒμΌμ—μ„œ store.dispatch(someAction) λ₯Ό ν˜ΈμΆœν•˜λ €λŠ” 경우 store μΈμŠ€ν„΄μŠ€μ— μ•‘μ„ΈμŠ€ν•˜λŠ” μ˜¬λ°”λ₯Έ 방법은 λ¬΄μ—‡μž…λ‹ˆκΉŒ?

μ§€κΈˆμ€ λ‹€μŒκ³Ό 같이 μž‘λ™ν•˜μ§€λ§Œ μ˜¬λ°”λ₯Έ μ ‘κ·Ό 방식인지 ν™•μ‹€ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

index.jsx :

...
const store = applyMiddleware(
  loggingMiddleware, // log every action
  thunk // making API requests from actions
)(createStore)(reducer);

export {
  store
};
...

Controls.jsx

import {store} from './index.jsx';
import * as actions from './actions';
...
let clickHandler = function(node, data) {
  store.dispatch(actions.nodeClicked(data))
}
export const Controls = React.createClass({
  render: function() {
    // React component gets rendered, and it passes the clickHandler to a 
    // function that attaches it to a D3 visualization
  }
});

이와 같은 상점을 수좜 / μˆ˜μž…ν•˜λŠ”λ° λ¬Έμ œκ°€ μžˆμŠ΅λ‹ˆκΉŒ? React μ»΄ν¬λ„ŒνŠΈμ˜ μΌλΆ€λ‘œ 클릭 ν•Έλ“€λŸ¬λ₯Ό μƒμ„±ν•˜κ³  this.context.store 호좜 ν•  수 μžˆλ‹€κ³  μƒκ°ν•˜μ§€λ§Œ μ‹€μ œλ‘œλŠ” D3 μ‹œκ°ν™”λ‘œ μ „λ‹¬λ˜λŠ” μ•½ 12 β€‹β€‹κ°œμ˜ λ‹€λ₯Έ 이벀트 ν•Έλ“€λŸ¬κ°€ μžˆμœΌλ―€λ‘œ μ‹€μ œλ‘œλŠ” μ‹€μš©μ μ΄μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. μƒνƒœ.

question

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

이와 같은 상점을 수좜 / μˆ˜μž…ν•˜λŠ”λ° λ¬Έμ œκ°€ μžˆμŠ΅λ‹ˆκΉŒ?

μ„œλ²„μ—μ„œ μš”μ²­λ§ˆλ‹€ λ‹€λ₯Έ store μΈμŠ€ν„΄μŠ€λ₯Ό κ°–κΈ°λ₯Ό μ›ν•˜κΈ° λ•Œλ¬Έμ— μ„œλ²„μ—μ„œ 앱을 λ Œλ”λ§ν•˜λŠ” 경우 μž‘λ™ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. 이것이 μš°λ¦¬κ°€ λ¬Έμ„œμ—μ„œμ΄ μ ‘κ·Ό 방식을 ꢌμž₯ν•˜μ§€ μ•ŠλŠ” μ΄μœ μž…λ‹ˆλ‹€.

λŒ€μ‹  μ΄κ²ƒμ„ν•˜μ§€ μ•ŠλŠ” μ΄μœ λŠ” λ¬΄μ—‡μž…λ‹ˆκΉŒ?

import { Component } from 'react';
import { connect } from 'react-redux';
import * as actions from './actions';

let createHandlers = function(dispatch) {
  let onClick = function(node, data) {
    dispatch(actions.nodeClicked(data))
  };

  return {
    onClick,
    // other handlers
  };
}

class Controls extends Component {
  constructor(props) {
    super(props);
    this.handlers = createHandlers(this.props.dispatch);
  }

  render() {
    // pass this.handlers anywhere you'd like
  }
}

export default connect()(Controls);

λͺ¨λ“  10 λŒ“κΈ€

λ””μŠ€νŒ¨μΉ˜λ₯Ό ​​ꡬ성 μš”μ†Œμ— λ°”μΈλ”©ν•˜λŠ” λ„μš°λ―Έ λ©”μ„œλ“œλŠ” https://github.com/rackt/react-redux λ₯Ό ν™•μΈν•˜μ‹­μ‹œμ˜€.

특히 https://redux.js.org/docs/basics/UsageWithReact.htmlμ—μ„œ

connect () 호좜둜 λž˜ν•‘ 된 λͺ¨λ“  ꡬ성 μš”μ†ŒλŠ” λ””μŠ€νŒ¨μΉ˜ ν•¨μˆ˜λ₯Ό prop으둜 μˆ˜μ‹ ν•˜κ³  μ „μ—­ μƒνƒœμ—μ„œ ν•„μš”ν•œ λͺ¨λ“  μƒνƒœλ₯Όλ°›μŠ΅λ‹ˆλ‹€.

  • μ½˜ν…μΈ κ°€ 더 이상 μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” κ΄€λ ¨ νŽ˜μ΄μ§€μ— λŒ€ν•œ μƒˆ 링크둜 μ—…λ°μ΄νŠΈ 된 λŒ“κΈ€

이와 같은 상점을 수좜 / μˆ˜μž…ν•˜λŠ”λ° λ¬Έμ œκ°€ μžˆμŠ΅λ‹ˆκΉŒ?

μ„œλ²„μ—μ„œ μš”μ²­λ§ˆλ‹€ λ‹€λ₯Έ store μΈμŠ€ν„΄μŠ€λ₯Ό κ°–κΈ°λ₯Ό μ›ν•˜κΈ° λ•Œλ¬Έμ— μ„œλ²„μ—μ„œ 앱을 λ Œλ”λ§ν•˜λŠ” 경우 μž‘λ™ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. 이것이 μš°λ¦¬κ°€ λ¬Έμ„œμ—μ„œμ΄ μ ‘κ·Ό 방식을 ꢌμž₯ν•˜μ§€ μ•ŠλŠ” μ΄μœ μž…λ‹ˆλ‹€.

λŒ€μ‹  μ΄κ²ƒμ„ν•˜μ§€ μ•ŠλŠ” μ΄μœ λŠ” λ¬΄μ—‡μž…λ‹ˆκΉŒ?

import { Component } from 'react';
import { connect } from 'react-redux';
import * as actions from './actions';

let createHandlers = function(dispatch) {
  let onClick = function(node, data) {
    dispatch(actions.nodeClicked(data))
  };

  return {
    onClick,
    // other handlers
  };
}

class Controls extends Component {
  constructor(props) {
    super(props);
    this.handlers = createHandlers(this.props.dispatch);
  }

  render() {
    // pass this.handlers anywhere you'd like
  }
}

export default connect()(Controls);

@gaearon λ©‹μ§€λ„€μš” , κ°μ‚¬ν•©λ‹ˆλ‹€!

@gaearon μ‹€μ œλ‘œ μ‚¬μš© this.props.dispatch undefined λ₯Ό λ°›κ³  μžˆμŠ΅λ‹ˆλ‹€.

λ‚΄ μ½”λ“œμ˜ κ΄€λ ¨ λΆ€λΆ„ :

import React from 'react';
import {Component} from 'react';
import {connect} from 'react-redux';
import * as vizActions from './vizActions';
import viz from './lib/viz';

let createHandlers = function(dispatch) {
  return {
    click: function(DOMNode, data) {
        // Getting undefined here
        dispatch(vizActions.setSelectedNode(data));
    }
  };
}

class Viz extends Component {
  constructor(props) {
    super(props);
    console.log(this.props.dispatch); // <- Getting undefined here
    // Passing the redux dispatch to be used in handlers function
    this.handlers = createHandlers(this.props.dispatch);
  }

  componentDidMount() {
    // Create Viz
  }

  componentDidUpdate() {
    // Update Viz Data, passing in this.handlers
  }

  render() {
    return <div id="viz"></div>;
  }
};


function mapStateToProps(state) {
  return {
    // Pass the network used for the viz as a prop
    network: state.get('visualization').get('network')
  };
}

export const VizContainer = connect(mapStateToProps, vizActions)(Viz);

κ·ΈλŸ¬λ‚˜ 또 λ‹€λ₯Έ 문제λ₯Ό 읽은 ν›„ (https://github.com/rackt/redux/issues/239) λ‚˜λŠ” μ‹€μ œλ‘œ λ‚΄ 행동 κ²°ν•©ν•˜κ³  μžˆμŒμ„ κΉ¨λ‹¬μ•˜λ‹€ this.props λ₯Ό 톡해 connect() λ‚΄κ°€ ν•  수 μžˆμœΌλ―€λ‘œ, createHandlers() 호좜 ν•  λ•Œ ν•„μš”ν•œ 쑰치λ₯Ό μ „λ‹¬ν•˜κΈ° λ§Œν•˜λ©΄λ©λ‹ˆλ‹€. μ•„λ§ˆλ„ 이것이 μ΅œμ„ μ˜ 방법은 μ•„λ‹ˆμ§€λ§Œ μ§€κΈˆμ€ μž‘λ™ν•©λ‹ˆλ‹€.

...
let createHandlers = function(action) {
  return {
    click: function(DOMNode, data) {
                // This works now
        action(data);
    }
  };
}

class Viz extends Component {
  constructor(props) {
    super(props);
    // Passing the redux dispatch to be used in handlers function
    this.handlers = createHandlers(this.props.setSelectedNode); // passing action creator function
  }
...

μ‹€μ œλ‘œ μ‹œλ„ν•œ ν›„ this.props.dispatch에 λŒ€ν•΄ μ •μ˜λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.

λ‚΄κ°€ κ²Œμ‹œ ν•œ μ½”λ“œλ₯Ό μ‹€ν–‰ν•˜κ³  μžˆμ§€ μ•ŠκΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€. λ‚΄κ°€ κ²Œμ‹œ ν•œ μ½”λ“œμ—μ„œ connect λŠ” 두 번째 맀개 λ³€μˆ˜λ₯Όλ°›μ§€ λͺ»ν–ˆμŠ΅λ‹ˆλ‹€. vizActions ν†΅κ³Όν•©λ‹ˆλ‹€. κ·Έλ ‡κΈ° λ•Œλ¬Έμ— dispatch 받지 λͺ»ν•©λ‹ˆλ‹€. connect ν†΅ν™”μ—μ„œ vizActions λ₯Ό μ œκ±°ν•˜λ©΄λ°›μ„ 수 μžˆμŠ΅λ‹ˆλ‹€.

두 가지 방법 λͺ¨λ‘ λ™μΌν•˜λ―€λ‘œ κ°€μž₯ 잘 μ½λŠ” 방법을 μ‚¬μš©ν•˜μ‹­μ‹œμ˜€.

: +1 : 이제 말이 λ˜λ„€μš”. 도와 μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€!

νŒŒν‹°μ— λŠ¦μ—ˆμ§€λ§Œ λ‚˜μ€‘μ— λˆ„κ΅°κ°€ (λ‚˜λ₯Ό ν¬ν•¨ν•˜μ—¬) 도움이 될 경우λ₯Ό λŒ€λΉ„ν•˜μ—¬ λ‹΅μž₯ν•©λ‹ˆλ‹€.
μ‚¬μš©μž μ •μ˜ mapDispatchToProps 이 ν•„μš”ν•œ 경우 (연결을 μœ„ν•΄ 전달 된 두 번째 맀개 λ³€μˆ˜) dispatch λ₯Ό ν‚€ 쀑 ν•˜λ‚˜λ‘œ λͺ…μ‹œ 적으둜 μΆ”κ°€ν•©λ‹ˆλ‹€.

const mapDispatchToProps = (dispatch) => {
  return {
    dispatch,

    // ... other custom mapped dispatch functions ...
    myFunction: (myParam) => {
      dispatch(State.Actions...)
    },
  }
}

export default connect( mapStateToProps, mapDispatchToProps )(Viz);

μ•ˆλ…•ν•˜μ„Έμš”. λ‚˜λŠ” λ˜ν•œμ΄ λ¬Έμ œκ°€ μžˆμŠ΅λ‹ˆλ‹€.
@gaearon , κ·€ν•˜μ˜ μ•ˆλ‚΄μ— κ°μ‚¬λ“œλ¦½λ‹ˆλ‹€. κ·€ν•˜μ˜ μ†”λ£¨μ…˜μ„ μ‚¬μš©ν–ˆμ§€λ§Œ μ΄ν•΄ν•˜κ³  μ‚¬μš©ν•  수 μ—†μŠ΅λ‹ˆλ‹€.
this.handlers μ‚¬μš© 방법을 λͺ¨λ₯΄κ² μŠ΅λ‹ˆλ‹€. μ‚¬μš©μžκ°€ μΉ΄λ“œλ₯Ό ν΄λ¦­ν•˜λ©΄ ν•΄λ‹Ή μΉ΄λ“œ IDλ₯Ό λΆ€λͺ¨μ˜ ꡬ성 μš”μ†Œλ‘œ 보내고 μž‘μ—…μ„ μ‹€ν–‰ν•©λ‹ˆλ‹€.

import React , { Component } from 'react';
import { Col } from "react-bootstrap";
import { connect } from 'react-redux';
import Home from "../components/Home";
import { fetchHome, asideArticle } from "../actions/index";

let createHandlers = function(dispatch) {
    let onClick = function(node, articleId) {
        dispatch(asideArticle(articleId))
    };
    return {
        onClick
    };
}

class HomeContainer extends Component {
    constructor(props) {
        super(props);
        this.handlers = createHandlers(this.props.dispatch); 
        console.log('constructor => this.props.dispatch => Result => dispatch is undefined);
    }
    componentDidMount() {
        this.props.fetchHome();
    }

    render() {
        const { homeData, article } = this.props;


       // 
        return (
            <Col>
                <Home homeData={homeData} asideArticle={(articleId) => asideArticle(articleId) } />
            </Col>
        );
    }
}

const mapStateToProps = state => ({
    homeData : state.home,
    article: state
});

function loadData(store){
    return store.dispatch(fetchHome());
}

export default {
    loadData,
    component: connect(mapStateToProps, { fetchHome, asideArticle })(HomeContainer)
}

this.handlers λ₯Ό μ‚¬μš©ν•˜κ³  클릭 이벀트λ₯Ό μ „λ‹¬ν•œ ν›„ articleId λ₯Ό μ „λ‹¬ν•˜μ—¬ μ „λ‹¬ν•˜λ €λ©΄ μ–΄λ–»κ²Œν•΄μ•Όν•©λ‹ˆκΉŒ?
node 맀개 λ³€μˆ˜λŠ” function(node, articleId) μ—μ„œ 무엇을 μ˜λ―Έν•˜λ©° μ–΄λ–€ μ—­ν• μ„ν•©λ‹ˆκΉŒ?

@EhsanFahami :

이것은 지원 μ‹œμŠ€ν…œμ΄ μ•„λ‹ˆλΌ 버그 μΆ”μ κΈ°μž…λ‹ˆλ‹€. μ‚¬μš©λ²•μ— λŒ€ν•œ μ§ˆλ¬Έμ€ 더 λ§Žμ€ μ‚¬λžŒλ“€μ΄ 도움을 쀄 μ€€λΉ„κ°€λ˜μ–΄μžˆλŠ” Stack Overflow λ˜λŠ” Reactifluxλ₯Ό μ‚¬μš©ν•˜μ‹­μ‹œμ˜€. μ•„λ§ˆλ„ 더 λ‚˜μ€ 닡변을 더 빨리 얻을 μˆ˜μžˆμ„ κ²ƒμž…λ‹ˆλ‹€. 감사!

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰