typeerror:无法读取未定义的react类组件的属性“onmouse”

iih3973s  于 2021-09-08  发布在  Java
关注(0)|答案(1)|浏览(494)

我想在用户单击输入元素时执行此操作,表单中的按钮元素将从麦克风图标更改为发送图标。我的想法是从onclick或mouseenter处理程序获取值,并通过if-else语句传递它,然后设置正确的图标
这是我的密码`

import React, { Component } from 'react'
import { Field, reduxForm } from 'redux-form';
import InsertEmoticonIcon from '@material-ui/icons/InsertEmoticon';
import AttachFileIcon from '@material-ui/icons/AttachFile';
import MicIcon from '@material-ui/icons/Mic';
import SendIcon from '@material-ui/icons/Send';

import styled from 'styled-components'
import './MessageSender.css'

export class MessageSender extends Component {

constructor(props){
    super(props);
    this.state = {
        bool: 'false'
    }
    this.onMouse = this.onMouse.bind(this);
}

onMouse(){
    this.setState({bool: "true"})
}

renderInput(formProps){

    return <input 
        onChange={formProps.input.onChange}
        value={formProps.input.value}
        placeholder="Message" 
        onClick={this.onMouse()}
    />
}

onSubmit(formValues){
    console.log(formValues);
}
check(){
    // return <Send />
    if(this.state.bool === 'true'){
        return <Send />
    }else{
        return <Mic />
    }
}

render() {
    return (
            <form autoComplete="off" onSubmit={this.props.handleSubmit(this.onSubmit)}>
                <Emotion />
                <Field  name="Message" component={this.renderInput} placeholder="Message"  />
                <Attach />
                <button>
                    {this.check()}
                </button>
            </form>

    )
}
}

const Emotion = styled(InsertEmoticonIcon)`
    width: 40px!important;
    height: 40px!important;
    color: rgb(170,170,170);
`

const Attach = styled(AttachFileIcon)`
    width: 40px!important;
    height: 40px!important;
    color: rgb(170,170,170);
    margin-right: 0.3rem;
`
const Mic = styled(MicIcon)`

`
const Send = styled(SendIcon)`

`

export default  reduxForm({
    form: 'message'
})(MessageSender);

`
因此,这是我的错误
请帮帮我,谢谢!

wpcxdonn

wpcxdonn1#

试试这个。我认为使用箭头函数可以解决使用 this . 当您将函数传递给时,不应该调用该函数 onClick ```
renderInput = (formProps) => {

return <input 
    onChange={formProps.input.onChange}
    value={formProps.input.value}
    placeholder="Message" 
    onClick={this.onMouse}
/>

}

相关问题