Issue with React.PropTypes.func.isRequired - reactjs

Am new in React and trying to define PropTypes, but seems it's no longer working :
Below is how I was working with it :
React.PropTypes.func.isRequired
Below is the error am getting :
Then this is the component am having what am I missing :
import React, {Component} from 'react';
import {Input,Icon,Row,Card, Button} from 'react-materialize'
import '../css/signup.css'
import PropTypes from 'prop-types';
class SignUpForm extends Component {
constructor(props) {
super(props);
this.state = {username: '', email:'', password:'', confirm_password:''};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({username: event.target.username});
this.setState({email: event.target.email});
this.setState({password: event.target.password});
this.setState({confirm_password: event.target.confirm_password});
}
handleSubmit(event) {
event.preventDefault();
this.props.userSignUpRequest(this.state);
}
render() {
return (
<div>
<Card className="card-effects right">
<form className="card-form-signup" onSubmit={this.handleSubmit}>
<Row>
<label className="signup-header"><b>Signup to Authors Haven</b></label>
</Row>
<Row>
<Input s={12} placeholder="Username" value={this.state.username} onChange={this.handleChange} validate>
<Icon className="icon-styles">account_box</Icon></Input>
</Row>
<Row>
<Input s={12} type='email' value={this.state.email} onChange={this.handleChange} placeholder="Email" validate><Icon className="green darken-4">email</Icon></Input>
</Row>
<Row>
<Input s={12} type='password' placeholder="Password" value={this.state.password} onChange={this.handleChange} validate>
<Icon className="icon-styles">vpn_key</Icon></Input>
</Row>
<Row>
<Input s={12} type='password' placeholder="Confirm password" value={this.state.confirm_password} onChange={this.handleChange} validate>
<Icon className="icon-styles">vpn_key</Icon></Input>
</Row>
<Row>
<label >Already have an account ? </label>
</Row>
<Row>
<Button className='button-effects' type="submit" value="Submit" > Signup </Button>
</Row>
</form>
</Card>
</div>
);
}
}
SignUpForm.propTypes = {
userSignUpRequest: React.PropTypes.func.isRequired
}
export default SignUpForm;

Depending on your React version PropTypes might be in a different package: https://www.npmjs.com/package/prop-types
import PropTypes from 'prop-types';
SignUpForm.propTypes = {
userSignUpRequest: PropTypes.func.isRequired
}

As the documentation states,
React.PropTypes has moved into a different package since React v15.5. Please use the prop-types library instead.
It's already imported:
import PropTypes from 'prop-types';
While the component still uses React.PropTypes.
It should be:
SignUpForm.propTypes = {
userSignUpRequest: PropTypes.func.isRequired
}

Related

TypeError: __WEBPACK_IMPORTED_MODULE_1__firebase__.a.auth(...).createUserWithEmailandPassword is not a function

I'm a beginner at React and Firebase, I'm facing this issue when I'm trying to register the user from my front end. Can anyone please help me out.? I'm using firebase version 6.0.2. Is there any other way to go about this..or do I downgrade to an older version of firebase.
Register.js
import React from 'react';
import firebase from '../../firebase';
import {Grid,Form,Segment,Button,Header,Message,Icon} from 'semantic-ui-react';
import {Link} from 'react-router-dom'
class Register extends React.Component{
state={
username:'',
email:'',
password:'',
passwordConfirmation:''
}
handleChange=event=>{
this.setState({
[event.target.name]:event.target.value
})
}
handleSubmit=event=>{
event.preventDefault();
firebase
.auth()
.createUserWithEmailandPassword(this.state.email,this.state.password)
.then(createdUser=>{
console.log(createdUser);
})
.catch(err=>{
console.log(err);
} )
}
render(){
const {username,email,password,passwordConfirmation}=this.state
return(
<Grid textAlign="center" verticalAlign="middle"className="app">
<Grid.Column style={{maxWidth:450}}>
<Header as="h2" icon color="orange" textAlign="center">
<Icon name="puzzle piece" color="orange"/>
Register for DevChat
</Header>
<Form onSubmit={this.handleSubmit}size="large">
<Segment stacked>
<Form.Input
fluid name="username"
icon="user" iconPosition="left"
placeholder="Username"
onChange={this.handleChange}
value={username}
type="text"/>
<Form.Input fluid name="email"
icon="mail" iconPosition="left"
placeholder="Email Address"
onChange={this.handleChange}
value={email}
type="email"/>
<Form.Input fluid name="password"
icon="lock" iconPosition="left"
placeholder="Password"
onChange={this.handleChange}
value={password}
type="password"/>
<Form.Input fluid name="passwordConfirmation"
icon="repeat"
iconPosition="left"
placeholder="Password Confirmation"
onChange={this.handleChange}
value={passwordConfirmation}
type="password"/>
<Button color="orange" fluid size="large">Submit</Button>
</Segment>
</Form>
<Message> Already a user?<Link to="/login">Login</Link> </Message>
</Grid.Column>
</Grid>
)
}
}
export default Register
firebase.js
import firebase from 'firebase/app';
import "firebase/auth"
import "firebase/database"
import "firebase/storage"
const firebaseConfig = {
//credentails
};
firebase.initializeApp(firebaseConfig);
export default firebase;
Thanks in advance!
Franks comment was correct you have a type error
Your handlesubmit:
handleSubmit=event=>{
event.preventDefault();
firebase
.auth()
.createUserWithEmailandPassword(this.state.email,this.state.password)
.then(createdUser=>{
console.log(createdUser);
})
.catch(err=>{
console.log(err);
} )
}
your handle submit should look like this or change you function to this
handleSubmit=event=>{
event.preventDefault();
firebase
.auth()
.createUserWithEmailAndPassword(this.state.email,this.state.password) //change in this line
.then(createdUser=>{
console.log(createdUser);
})
.catch(err=>{
console.log(err);
})
}
If Frank's solution worked for you just ignore this(as this is just the same but in code)

'ControlLabel' is not exported from 'react-bootstrap'

Hello I'm creating a login form in react, to check the login parameters use Checkbel of the package, but when I run the code I'm thrown this exception: ./src/App.js Attempted import error: 'ControlLabel' is not exported from 'react-bootstrap'. how do I solve?
React Code:
import React, { Component } from "react";
import { Form,Button, FormGroup, FormControl, ControlLabel } from "react-bootstrap";
import "./Login.css";
import Bootstrap from "react-bootstrap";
export default class Login extends Component {
constructor(props) {
super(props);
this.state = {
email: "",
password: ""
};
}
validateForm() {
return this.state.email.length > 0 && this.state.password.length > 0;
}
handleChange = event => {
this.setState({
[event.target.id]: event.target.value
});
}
handleSubmit = event => {
event.preventDefault();
}
render() {
return (
<div className="Login">
<form onSubmit={this.handleSubmit}>
<FormGroup controlId="email" bsSize="large">
<ControlLabel>Email</ControlLabel>
<FormControl
autoFocus
type="email"
value={this.state.email}
onChange={this.handleChange}
/>
</FormGroup>
<FormGroup controlId="password" bsSize="large">
<ControlLabel>Password</ControlLabel>
<FormControl
value={this.state.password}
onChange={this.handleChange}
type="password"
/>
</FormGroup>
<Button
block
bsSize="large"
disabled={!this.validateForm()}
type="submit"
>
Login
</Button>
</form>
</div>
);
}
}
Try FormLabel instead of ControlLabel.
<ControlLabel> is from an older version of react-bootstrap. The current version ("react-bootstrap": "^1.4.3") uses <FormLabel> or <Form.Label>
According to this url https://react-bootstrap.github.io/components/buttons/#button-props and your version you should modify your code like this.
import React, { Component } from "react";
import Form from 'react-bootstrap/Form'
import Button from 'react-bootstrap/Button'
import Bootstrap from "react-bootstrap";
export default class Login extends Component {
constructor(props) {
super(props);
this.state = {
email: "",
password: ""
};
}
validateForm() {
return this.state.email.length > 0 && this.state.password.length > 0;
}
handleChange = event => {
this.setState({
[event.target.id]: event.target.value
});
}
handleSubmit = event => {
event.preventDefault();
}
render() {
return (
<div className="Login">
<Form onSubmit={this.handleSubmit}>
<Form.Group controlId="email" bsSize="large">
<Form.Control
autoFocus
type="email"
value={this.state.email}
onChange={this.handleChange}
/>
</Form.Group>
<Form.Group controlId="password" bsSize="large">
<Form.Control
value={this.state.password}
onChange={this.handleChange}
type="password"
/>
</Form.Group>
<Button
block
bsSize="large"
disabled={!this.validateForm()}
type="submit"
>
Login
</Button>
</Form>
</div>
);
}
}
This "react-bootstrap": "^1.0.0-beta.16", doesn't contain the ControlLabel, so
uninstall it using
npm uninstall react-bootstrap
and then re install with this "react-bootstrap": "^0.32.4" version
npm install react-bootstrap#0.32.4 --save
will definitely fix this issue

"store" not found in either the context or props of "Connect(Signup)

Am trying to run enzyme tests but am facing this error,
Invariant Violation: Could not find "store" in either the context or
props of "Connect(Signup)". Either wrap the root component in a
, or explicitly pass "store" as a prop to "Connect(Signup)".
I used Redux in my application and it runs well in the browser but fails to run tests in enzyme and jest.
Below is my code :
For the test file below is the code in App.test.js.
import React from 'react';
import ReactDOM from 'react-dom';
import App from '../App'
it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
ReactDOM.unmountComponentAtNode(div);
});
Signup.js
import React, { Component } from 'react';
import NavBar from './subcomponents/NavBar'
import SignUpForm from './subcomponents/SignUpForm'
import { connect } from 'react-redux'
import {userSignUpRequest} from "./actions/signUpActions";
import PropTypes from 'prop-types';
class Signup extends Component {
render() {
const {userSignUpRequest} = this.props;
return (
<div>
<NavBar/>
<SignUpForm userSignUpRequest={userSignUpRequest}/>
</div>
);
}
}
Signup.propTypes = {
userSignUpRequest: PropTypes.func.isRequired
}
export default connect(null, { userSignUpRequest })(Signup);
SignUpForm.js
import React, {Component} from 'react';
import {Input, Icon, Row, Card, Button, ProgressBar, Col, Preloader} from 'react-materialize'
import '../css/signup.css'
import PropTypes from 'prop-types';
class SignUpForm extends Component {
constructor(props) {
super(props);
this.state = {
username: '',
email:'',
password:'',
confirm_password:'',
visible: true
}
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
const {name,value} = event.target;
this.setState({[name]: value});
}
handleSubmit(event) {
event.preventDefault();
console.log('my state ', this.state);
this.setState({visible: true});
if(this.state.password.trim() === this.state.confirm_password.trim()) {
this.props.userSignUpRequest(this.state);
}
else{
alert('No they don\'t match');
this.setState({visible: true});
}
}
componentDidMount() {
console.log('Parent did mount.');
document.getElementById('text_message').style.visibility = "hidden";
this.setState({visible: false});
}
render() {
const isVisible = this.state.visible;
return (
<div>
<Card className="card-effects right">
{isVisible && <ProgressBar id="progress_Bar" name="progress_Bar"/>}
<Row>
<label className="text-message" id="text_message" name="text_message"><i>text message</i></label>
</Row>
<form className="card-form-signup" onSubmit={this.handleSubmit}>
<Row>
<label className="signup-header"><b>Signup to Authors Haven</b></label>
</Row>
<Row>
<Input s={12} placeholder="Username" name="username" value={this.state.username} onChange={this.handleChange} validate>
<Icon className="icon-styles">account_box</Icon></Input>
</Row>
<Row>
<Input s={12} type='text' name="email" value={this.state.email} onChange={this.handleChange} placeholder="Email" validate><Icon className="green darken-4">email</Icon></Input>
</Row>
<Row>
<Input s={12} type='password' name="password" placeholder="Password" value={this.state.password} onChange={this.handleChange} validate>
<Icon className="icon-styles">vpn_key</Icon></Input>
</Row>
<Row>
<Input s={12} type='password' name="confirm_password" placeholder="Confirm password" value={this.state.confirm_password} onChange={this.handleChange} validate>
<Icon className="icon-styles">vpn_key</Icon></Input>
</Row>
<Row>
<label >Already have an account ? </label>
</Row>
<Row>
<Button className='button-effects' type="submit" value="Submit" > Sign up </Button>
</Row>
</form>
</Card>
</div>
);
}
}
SignUpForm.propTypes = {
userSignUpRequest: PropTypes.func.isRequired
}
export default SignUpForm;
In test you'll still need to mount the components inside a <Provider> so that all usages of connect() downstream of App can work properly.
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>, div);

Unexpected use of 'event' on Login

I'm running a react-create-app and it's giving me the following errors on my below Login.js
./src/components/Login/Login.js
Line 6: 'handleClick' is not defined no-undef
Line 6: Unexpected use of 'event' no-restricted-globals
I've tried moving the handleClick event elsewhere in the js file but I continue to get the same error.
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {Button, Checkbox, Col, ControlLabel, Form, FormControl, FormGroup} from 'react-bootstrap';
import './Login.css';
handleClick(event)
{
const username = this.refs.username;
const password = this.refs.password;
const creds = { username: username.value.trim(), password: password.value.trim() };
this.props.onLoginClick(creds)
}
Login.propTypes = {
onLoginClick: PropTypes.func.isRequired,
errorMessage: PropTypes.string
};
class Login extends Component {
render() {
const { errorMessage } = this.props;
return (
<Form horizontal>
<FormGroup controlId="formHorizontalEmail">
<Col componentClass={ControlLabel} sm={2}>
Email
</Col>
<Col sm={10}>
<FormControl type="email" placeholder="Email" />
</Col>
</FormGroup>
<FormGroup controlId="formHorizontalPassword">
<Col componentClass={ControlLabel} sm={2}>
Password
</Col>
<Col sm={10}>
<FormControl type="password" placeholder="Password" />
</Col>
</FormGroup>
<FormGroup>
<Col smOffset={2} sm={10}>
<Checkbox>Remember me</Checkbox>
</Col>
</FormGroup>
<FormGroup>
<Col smOffset={2} sm={10}>
<Button onClick={(event) => this.handleClick(event)} type="submit">Sign in</Button>
</Col>
</FormGroup>
{errorMessage &&
<p style={{color:'red'}}>{errorMessage}</p>
}
</Form>
);
}
}
export default Login;
This ended up working:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {Button, Checkbox, Col, ControlLabel, Form, FormControl, FormGroup} from 'react-bootstrap';
import './Login.css';
class Login extends Component {
render() {
const { errorMessage } = this.props;
Login.propTypes = {
onLoginClick: PropTypes.func.isRequired,
errorMessage: PropTypes.string
}
function handleClick(event) {
const username = this.refs.username;
const password = this.refs.password;
const creds = { username: username.value.trim(), password: password.value.trim() };
this.props.onLoginClick(creds)
}
return (
<Form horizontal>
<FormGroup controlId="formHorizontalEmail">
<Col componentClass={ControlLabel} sm={2}>
Email
</Col>
<Col sm={10}>
<FormControl type="email" placeholder="Email" />
</Col>
</FormGroup>
<FormGroup controlId="formHorizontalPassword">
<Col componentClass={ControlLabel} sm={2}>
Password
</Col>
<Col sm={10}>
<FormControl type="password" placeholder="Password" />
</Col>
</FormGroup>
<FormGroup>
<Col smOffset={2} sm={10}>
<Checkbox>Remember me</Checkbox>
</Col>
</FormGroup>
<FormGroup>
<Col smOffset={2} sm={10}>
<Button onClick={(event) => handleClick(event)} type="submit">Sign in</Button>
</Col>
</FormGroup>
{errorMessage &&
<p style={{color:'red'}}>{errorMessage}</p>
}
</Form>
)
}
}
export default Login;
Moving handleClick inside the class fixes the error:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {Button, Checkbox, Col, ControlLabel, Form, FormControl, FormGroup} from 'react-bootstrap';
import './Login.css';
Login.propTypes = {
onLoginClick: PropTypes.func.isRequired,
errorMessage: PropTypes.string
};
class Login extends Component {
handleClick(event)
{
const username = this.refs.username;
const password = this.refs.password;
const creds = {
username: username.value.trim(),
password: password.value.trim()
};
this.props.onLoginClick(creds)
}
render() {
const { errorMessage } = this.props;
return (
<Form horizontal>
<FormGroup controlId="formHorizontalEmail">
<Col componentClass={ControlLabel} sm={2}>
Email
</Col>
<Col sm={10}>
<FormControl type="email" placeholder="Email" />
</Col>
</FormGroup>
<FormGroup controlId="formHorizontalPassword">
<Col componentClass={ControlLabel} sm={2}>
Password
</Col>
<Col sm={10}>
<FormControl type="password" placeholder="Password" />
</Col>
</FormGroup>
<FormGroup>
<Col smOffset={2} sm={10}>
<Checkbox>Remember me</Checkbox>
</Col>
</FormGroup>
<FormGroup>
<Col smOffset={2} sm={10}>
<Button onClick={(event) => this.handleClick(event)} type="submit">Sign in</Button>
</Col>
</FormGroup>
{errorMessage &&
<p style={{color:'red'}}>{errorMessage}</p>
}
</Form>
);
}
}
export default Login;

react-bootstrap - login form - Uncaught TypeError

I have the following login.js
import React, { Component, PropTypes } from 'react';
import { Button, Form, FormControl, FormGroup } from 'react-bootstrap';
export default class Login extends Component {
render() {
const {errorMessage} = this.props
return (
<Form inline>
<FormGroup controlId="formInlineEmail">
<FormControl type="email" ref="username" placeholder="Email"/>
</FormGroup>
<FormGroup controlId="formInlinePassword">
<FormControl type="password" ref="password"/>
</FormGroup>
<Button type="submit" onClick={(event) => this.handleClick(event)}>
Login
</Button>
{errorMessage &&
<p style={{color:'red'}}>{errorMessage}</p>
}
</Form>
)
}
handleClick(event) {
const username = this.refs.username
const password = this.refs.password
const creds = { username: username.value.trim(), password: password.value.trim() }
this.props.onLoginClick(creds)
}
}
Login.propTypes = {
onLoginClick: PropTypes.func.isRequired,
errorMessage: PropTypes.string
}
When attempting to login I get the following error:
Uncaught TypeError: (0, _reactDom2.default) is not a function at Login.handleClick
if I avoid using react-bootstrap and do the following in my Login.js it works fine.
<div>
<input type='text' ref='username' className="form-control" style={{ marginRight: '5px' }} placeholder='Username'/>
<input type='password' ref='password' className="form-control" style={{ marginRight: '5px' }} placeholder='Password'/>
<button onClick={(event) => this.handleClick(event)} className="btn btn-primary">
Login
</button>
{errorMessage &&
<p style={{color:'red'}}>{errorMessage}</p>
}
</div>
Don't ask why but this works:
import React, { Component, PropTypes } from 'react';
import { findDOMNode } from 'react-dom';
import { Button, ControlLabel, Form, FormControl, FormGroup } from 'react-bootstrap';
export default class Login extends Component {
handleSubmit(event) {
const username = findDOMNode(this.refs.username)
const password = findDOMNode(this.refs.password)
const creds = { username: username.value.trim(), password: password.value.trim() }
this.props.onLoginClick(creds)
}
render() {
const {errorMessage} = this.props
return (
<Form inline>
<FormGroup controlId="formHorizontalEmail">
<ControlLabel>Email </ControlLabel>
<FormControl type="username" ref="username" onChange={this.handleChange} placeholder="Email" />
</FormGroup>
<FormGroup controlId="formHorizontalPassword">
<ControlLabel>Password </ControlLabel>
<FormControl type="password" ref="password" onChange={this.handleChange} placeholder="Password" />
</FormGroup>
<Button onClick={(event) => this.handleSubmit(event)}>Login</Button>
{errorMessage &&
<p style={{color:'red'}}>{errorMessage}</p>
}
</Form>
)
}
}
Login.propTypes = {
onLoginClick: PropTypes.func.isRequired,
errorMessage: PropTypes.string
}

Resources