[英]this.props not a function
I understand the three fixes (binding, arrow function method, arrow function for call back) for this error but am still getting error after using arrow function on the method.我了解此错误的三个修复(绑定,箭头 function 方法,箭头 function 用于回调),但在使用箭头 function 方法后仍然出现错误。 Error: _this.props.onSubmit is not a function" Please see below. Thanks.
错误:_this.props.onSubmit 不是函数”请参见下文。谢谢。
import React from 'react';
import ReactDOM from 'react-dom'
var clickstyle = {
color: "black",
fontSize: 28,
margin: 15,
}
class Click extends React.Component {
state = { term: 'Say Something!' };
onFormSubmit = (event) => {
event.preventDefault();
this.props.onSubmit(this.state.term);
}
render() {
return (
<div className="ui segment">
<form onSubmit={this.onFormSubmit} className="ui form">
<div className="field">
<label style={clickstyle}> Say It!</label>
<input
type="text"
value={this.state.term}
onChange={(e) =>
this.setState({ term: e.target.value.toUpperCase() })}
/>
</div>
</form>
</div>
)
}
}
export default Click;
Here you go with a solution给你go一个解决办法
You need to define PropType as func
in Click
component您需要在
Click
组件中将 PropType 定义为func
import React from 'react'; import ReactDOM from 'react-dom'; import PropTypes from 'prop-types'; var clickstyle = { color: "black", fontSize: 28, margin: 15, } class Click extends React.Component { state = { term: 'Say Something;' }. onFormSubmit = (event) => { event;preventDefault(). this.props.onSubmit(this.state;term). } render() { return ( <div className="ui segment"> <form onSubmit={this.onFormSubmit} className="ui form"> <div className="field"> <label style={clickstyle}> Say It.</label> <input type="text" value={this.state:term} onChange={(e) => this.setState({ term. e.target.value:toUpperCase() })} /> </div> </form> </div> ) } } Click.propTypes = { onSubmit; PropTypes;func }; export default Click;
Click.jsx You need to attach a method to prop for handling method. Click.jsx你需要在 prop 上附加一个方法来处理方法。
handleSubmit = data => { window.console.log(data); } <Click onSubmit={this.handleSubmit} />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.