I have three components, one controller, and two childrens of this controller. For this problem we need a controller and one children (his name is : Input.js)
So, I expose the problem. In my Input.js
I call the onChange()
method and updated inside of this file the state and in my controller I passing a props for recover the the state in my children.
But I have an error this.props.myName is not a function
Please you can take a look of this code inside :
** Input.js **
import React, { Component, Fragment } from "react";
class Input extends Component {
constructor(props) {
super(props);
console.log(props);
this.state = {
email: "",
password: ""
};
_handleChange(pEvt) {
if (pEvt.target.type === "email" && pEvt.target.value.length >= 6) {
this.setState({
email: pEvt.target.value
});
} else {
this.setState({
password: pEvt.target.value
});
}
}
render() {
const { type, placeholder } = this.props;
return (
<Fragment>
<div className="form__group">
<input
className="form__input"
type={type}
placeholder={placeholder}
ref={input => (this.refInput = input)}
onChange={pEvt => {
this._handleChange(pEvt);
}}
/>
</div>
</Fragment>
);
}
}
export default Input;
** SignInUp.js (Controller) **
import React, { Component } from "react";
import Header from "./../Header/Header";
import Input from "./../Input/Input";
import Submit from "./../Input/Submit";
import "./_SignInUp.scss";
class sign extends Component {
constructor(props) {
super(props);
this.state = {
input: []
};
}
_inputValue = email => {
this.setState({
input: [...this.state.input, email]
});
};
render() {
return (
<div className="sign">
<Header />
<form className="form">
<Input
inputValue={this._inputValue}
type="email"
placeholder="Email"
/>
<Input type="password" placeholder="Password" />
<div className="form__submit">
<Submit name="Sign in" to="/profile" />
<Submit name="Sign Up" to="/" />
</div>
</form>
</div>
);
}
}
export default sign;