简体   繁体   English

获取TypeError:_red2.props.handleClick不是一个函数,当将redux prop从容器传递到react中的表示组件时

[英]Getting TypeError: _this2.props.handleClick is not a function when passing redux prop from container to presentational component in react

Pretty new to Redux. Redux的新手。 I'm trying to pass a handleClick event as a prop from a container component to a presentational component, the handleClick event is supposed to call upon an action which has been received as a prop with mapDispatchToProps. 我正在尝试将handleClick事件作为道具从容器组件传递到表示组件,handleClick事件应该调用通过mapDispatchToProps作为道具收到的动作。

Could someone tell me how to do this correctly please? 有人可以告诉我如何正确执行此操作吗?

I'm building a calculator, just started, this only has three actions so far, add, Record_input_1 and Record_Input_2. 我正在构建一个计算器,刚刚开始,到目前为止只有3个操作,add,Record_input_1和Record_Input_2。

containers/ButtonsContainer.js: 集装箱/ ButtonsContainer.js:

import React, { Component } from 'react';
import { Buttons } from '../components/Buttons'
import { Record_Input_1 } from '../actions/sum-action';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';


class ButtonsContainer extends Component {
    constructor(props) {
        super(props);
        this.handleClick = this.handleClick.bind(this);
    }

    handleClick(num) {
        return this.props.onRecordInput1(num)
    }

    render() {
        return(
            <Buttons handleClick={this.handleClick} />
     )
    }

    mapStateToProps = (state) => {
        return {
            inputValue1: state.inputValue1,
            inputValue2: state.inputValue2,
            answer: state.answer
        }
    }

    mapDispatchToProps = (dispatch) => {
        return bindActionCreators({
            onRecordInput1: Record_Input_1,
            onRecordInput2: Record_Input_2
        }, dispatch);
    }
}

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

components/Buttons.js 组件/ Buttons.js

import React, { Component } from 'react';



class Buttons extends Component {


    render() {
        const buttonMaker = (buttons, row) => {
            for (let value of buttons) {
                row.push(<button onClick={() => this.props.handleClick(value)} key={value}>
                {value}
                </button> )
              }
        }

        let row1 = [];
        let buttons1 = [1,2,3]
        buttonMaker(buttons1, row1)


        let row2 = [];
        let buttons2 = [4,5,6]
        buttonMaker(buttons2, row2)


        let row3 = [];
        let buttons3 = [7,8,9]
        buttonMaker(buttons3, row3)

        return (
            <div>
          <div>{row1}</div>
          <br />
          <div>{row2}</div>
          <br />
          <div>{row3}</div>

        </div>
        )
    }  
}



export default Buttons;

actions/sum-actions/js:

export const ADD = 'ADD';
export const RECORD_INPUT_1 = 'RECORD_INPUT_1';
export const RECORD_INPUT_2 = 'RECORD_INPUT_2';

export const add = (newInput1, newInput2) => {
    return {
        type: ADD,
        newAnswer: newInput1 + newInput2
    }
}

export const Record_Input_1 = (newInput1) => {
    return {
        type: RECORD_INPUT_1,
        newInput1
    }
}

export const Record_Input_2 = (newInput2) => {
    return {
        type: RECORD_INPUT_2,
        newInput2
    }
}

reducders/sum-reducer.js: reducders /总和-reducer.js:

import { ADD, RECORD_INPUT_1, RECORD_INPUT_2 } from '../actions/sum-action'

export const initialState = {
    inputValue1: '',
    inputValue2: '',
    answer: 0
}

export const sumReducer = (state = initialState, action) => {
    switch (action.type) {
        case ADD:
        return [
            ...state,
            {
                answer: action.newAnswer
            }
        ]
        case RECORD_INPUT_1:
        return [
            ...state, 
            {
                inputValue1: action.newInput1
            }
        ]
        case RECORD_INPUT_2:
        return [
            ...state, 
            {
                inputValue2: action.newInput2
            }
        ]
        default:
        return state;
    }
}

store.js: store.js:

import { combineReducers, createStore } from 'redux';
import { initialState, sumReducer } from './reducers/sum-reducer';


const rootReducers = combineReducers({
    sumReducer
})

export default createStore(rootReducers, initialState, window.devToolsExtension && window.devToolsExtension());

The buttons display ok, when I click on one I get this error: TypeError: _this2.props.handleClick is not a function for: 按钮显示确定,当我单击一个按钮时出现以下错误: TypeError:_this2.props.handleClick不是以下功能

  8 | render() {
   9 |     const buttonMaker = (buttons, row) => {
  10 |         for (let value of buttons) {
> 11 |             row.push(<button onClick={() => this.props.handleClick(value)} key={value}
  12 |             {value}
  13 |             </button> )
  14 |           }

You are declaring mapStateToProps and mapDispatchToProps within ButtonsContainer . 您声明mapStateToPropsmapDispatchToPropsButtonsContainer You are then passing those two methods to react-redux's connect as if they were declared outside of ButtonsContainer , hence they are undefined . 然后,您要将这两个方法传递给react-redux的connect ,就好像它们是在ButtonsContainer之外声明的,因此它们是undefined Try moving them out of ButtonsContainer as shown here . 尝试移动出来的ButtonsContainer如图所示这里 It should look something like this: 它看起来应该像这样:

class ButtonsContainer extends Component {
    ...
}

const mapStateToProps = (state) => {
    return {
        inputValue1: state.inputValue1,
        inputValue2: state.inputValue2,
        answer: state.answer
    }
}

const mapDispatchToProps = (dispatch) => {
    return bindActionCreators({
        onRecordInput1: Record_Input_1,
        onRecordInput2: Record_Input_2
    }, dispatch);
 }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Redux时,将事件从表示形式传递到容器组件是否是反模式? - Is passing an event from a presentational to container component an anti-pattern when using Redux? React &amp; Redux - 展示组件中的容器组件? - React & Redux - container component inside a presentational component? React Redux:从容器到组件传递道具和分派的问题 - React Redux : Problem with passing props and dispatch from container to component 反应表示组件无法读取值 <input /> 来自redux商店使用反应容器 - React presentational component unable to read value for <input /> from redux store using react container 未捕获的TypeError:从Redux容器调用函数时,React-Redux应用程序中的this.props。***不是函数 - Uncaught TypeError: this.props.*** is not a function in React-redux app while calling a function from redux container React Redux-从reducer传递数据时容器/组件未更新 - React Redux - container/component not updating when passing data from reducer 使用 handleClick 从 React 中的卡片组件获取道具 - Using handleClick to get props from a card component in React 在React Native中将容器组件与表示组件分离 - separating container component from presentational component in react native React / Redux-将道具从一个容器传递到另一个 - React/Redux - Passing props from one container to another Redux 展示组件与容器组件 - Redux Presentational Components Vs Container Component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM