组件props.dispatch不起作用。 React redux

时间:2016-12-20 10:34:40

标签: javascript reactjs ecmascript-6 redux react-jsx

我需要帮助。在我的色彩控制中,我试图做一个没有运气的this.props.dispatch(triggerFBEvent(fbID, method, params))。 但是,如果我只做triggerFBEvent(fbID, method, params)那么有效。我收到错误: index.bundle.js:61968 Uncaught TypeError: this.props.dispatch is not a function

我想要完成的是能够使用上面的行发送新的道具,然后

componentWillMount() { this.props.dispatch(fetchFBEvent(this.props.fbID, "getColor")) }

调用自定义服务以使用适当的颜色更新状态。但是this.props.dispatch也不是函数。

import React from 'react'
import { connect } from 'react-redux'
import {triggerFBEvent, triggerFBClearEvent, fetchFBEvent} from '../actions/functionblocksActions'
`
import { CustomPicker, HuePicker, SaturationPicker, SliderPicker, CustomPointer } from 'react-color';


@connect((store) => {
  return {
    fb: store.functionblocks.functionblock
  }
})

export default class Functionblock extends React.Component {

  constructor(props) {
    super(props);
      this.state = {

      };
    this._getType = this._getType.bind(this)

  }
      _getType (wanted) {
        const { fbID, fbName, func } = this.props;
        let type;
        let types = {
          'com.xxxx.xx.service.dal.functions.Alarm': function () {
            type = <Alarm fbID={fbID} fbName={fbName}/>;
          },
          'com.xxxx.xxx.service.dal.functions.BooleanControl': function () {
            type = <BooleanControl fbID={fbID} fbName={fbName}/>;
          },
          'com.xxx.xxxx.service.dal.functions.BooleanSensor': function () {
            type = <BooleanSensor fbID={fbID} fbName={fbName} />;
          },
          'com.xxxx.xxx.service.dal.functions.ColorControl': function () {
            type = <ColorControl func={func} fbID={fbID} fbName={fbName} />;
          }

          'default': function () {
            type = <WakeUp fbID={fbID} fbName={fbName} />;
          }
        };

        // invoke it
        (types[wanted] || types['default'])();

        // return a String with chosen type
        return type;
      }

  render() {
    const { fbID, fbName, func } = this.props;
    const type = this._getType(func.serviceProperties["clazz"]);


      return( 
        <div>
        {type}
        </div>
      )
  }
}


// Classes for the different functions.
class ColorControl extends React.Component {
    componentWillMount() {
      this.props.dispatch(fetchFBEvent(this.props.fbID, "getColor"))
    }
    constructor(props) {
        super(props);
        this.state = {
          color: {
            h: 150.3197479248047,
            s: 0.5,
            l: 0.5
          }
        }
        this.onChangeComplete = this.onChangeComplete.bind(this);
    }
    componentWillReceiveProps(nextProps) {
      alert("YEP")
    // let home = this._getHome(nextProps.areas, nextProps.statics);
    // if(home!=null){
    //   this.setState({
    //     inputHome: home.name,
    //   })
    // }
    }
    onChangeComplete(color, event) {

        let hsl = color.hsl;

        let hue = color.hsl.h / 360;
        let saturation = color.hsl.s;
        let lightness = color.hsl.l;

        this.setState({ color: hsl })

        // Update props
        let fbID = this.props.fbID;
        let method = "setColor";
        let params = {"hue": hue, "sat": saturation, "light": lightness};
        this.props.dispatch(triggerFBEvent(fbID, method, params))

      }
    _defineFunction(){

    }

    render() {
        return (<div>
          <SliderPicker {...this.props}
          pointer={ CustomPointer }
          color={this.state.color}
          onChangeComplete={ this.onChangeComplete }
          direction={ 'horizontal' || 'vertical' }/>

        </div>
        )
    }
}

任何人都可以帮我理解什么是错的吗?

3 个答案:

答案 0 :(得分:1)

您需要将ColorControl连接到Redux,否则它无法获得调度支持。

@connect()
class ColorControl extends React.Component {

答案 1 :(得分:1)

以下是使用您的操作而没有问题的代码库。

import * as actions from './YourActionsPath'
import { bindActionCreators } from 'redux'

@connect(  
  state => ({
    yourDerivedState : state.somePath
  }),
  dispatch => ({
    actions : bindActionCreators( actions, dispatch )
  })
)

export default class YourClass extends Component {
  someMethod(){
    this.props.actions.yourAction() // call it with no problems
  }
  render(){
    return (
      // your html
    )
  }
}

我希望你明白这个主意。如果你使用这种模式,你就不会有问题。

由于您可以将派生状态用作在连接中定义的this.props.derivedState,您还可以使用在连接上定义的操作。

此外,如果连接了组件,则可以使用this.props.dispatch。如果您需要它,就像您的情况一样,但这会使代码不那么清晰并导致维护问题。

答案 2 :(得分:-1)

import { createStore } from 'redux'
let store = createStore(//define reducer,preload state and enhancer)

//call action 
store.dispatch(triggerFBEvent(fbID, method, params))