简体   繁体   English

TypeError:this.props.function不是函数

[英]TypeError: this.props.function is not a function

First of all, I would like to say that I know there are many questions regarding this topic. 首先,我想说的是,我知道关于这个话题有很多问题。 I read through most of them and I double and triple checked my code but I can't seem to get it to work. 我通读了其中的大部分内容,并仔细检查了我的代码,但似乎无法正常工作。

I have the following child component, which contains an image, which you can change by clicking on said image (you can iterate through an array of 5 images). 我有以下子组件,其中包含一个图像,您可以通过单击该图像进行更改(可以迭代5个图像的数组)。

export default class Images extends React.Component {
    constructor(props) {
        super(props);
        this.changeIcon = this.changeIcon.bind(this);
        this.state = {pointer: this.props.id-1, imgs: props.imgs };
    }

    changeIcon () {
        const {length} = this.state.imgs;
        const {pointer } = this.state;
        const newPointer = pointer === length - 1 ? 0 : pointer+1;
        this.setState({ pointer: newPointer });
        this.props.onChangeIcon("I work");
    }

    render() {
        const isclicked = this.props.clicked;
        const { pointer, imgs } = this.state;
        return(
            <img src={imgs[pointer]} onClick={this.changeIcon}/>
        );
    }
}

This is the parent component: 这是父组件:

import Images from "./images";

class Displayer extends React.Component {
    constructor(props) {
        super(props);
        this.changeIcons = this.changeIcons.bind(this);
        this.state = {
            imgs = [link1, link2, link3, link4, link5]
        }
    }
    changeIcons(word) {
        console.log(word);
    }

    render () {
        return (
            <div>
                <Images onChangeIcon={this.changeIcons} imgs={this.state.imgs}/>
            </div>
        )
    }

}

The weird thing is, that it works perfectly fine with another function inside the same child component. 奇怪的是,它与同一子组件中的另一个函数完美配合。 I used the same structure and I also double-checked for any spelling mistakes, but nothing helped. 我使用相同的结构,并且还仔细检查了拼写错误,但没有任何帮助。

I still receive the same error message TypeError: this.props.changeIcon is not a function . 我仍然收到相同的错误消息TypeError: this.props.changeIcon is not a function Am I missing something? 我想念什么吗? Can anyone spot a mistake? 谁能发现一个错误?

Edit: the id-prop is added at another point. 编辑:id-prop添加在另一点。 In order so keep it simple I did not include that. 为了简单起见,我没有将其包括在内。

changeIcons doesn't belong to props but just a method of your component changeIcons不属于道具,而只是组件的一种方法

Instead of this: 代替这个:

<Images onChangeIcon={this.props.changeIcons}

Try this: 尝试这个:

<Images onChangeIcon={this.changeIcons}

UPDATE: 更新:

Try to use arrow function in the callback to see where the problem is: 尝试在回调中使用箭头函数以查看问题所在:

return (
    <img src={imgs[pointer]} onClick={() => this.changeIcon() }/>
);

Or just turn changeIcon into arrow function: 或者只是将changeIcon变成箭头功能:

changeIcon = () => {
    const {length} = this.state.imgs;
    const {pointer } = this.state;
    const newPointer = pointer === length - 1 ? 0 : pointer+1;
    this.setState({ pointer: newPointer });
    this.props.onChangeIcon("I work");
}

render() {
    const isclicked = this.props.clicked;
    const { pointer, imgs } = this.state;
    return(
        <img src={imgs[pointer]} onClick={this.changeIcon}/>
    );
}

First thing you have done mistake in constructor function. 首先,您在构造函数中犯了错误。 pass imgs and ids also as you are using in child component. 像在子组件中一样使用imgs和id。 Do it like this 像这样做

import Images from "./images";

class Displayer extends React.Component {
    changeIcons = (word) => {
        console.log(word);
    }
    state = {
     imgs : ["img1","img2", "img3"],
     id: 0,
    }
//Here i dont know how you are getting img urls array.Please add your code accordingly
    render () {
        return (
            <div>
                <Images imgs={this.state.imgs} onChangeIcon={this.changeIcons} />
            </div>
        )
    }

}

And thats it, now it should work. 就是这样,现在应该可以了。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM