I am having problems with setState - node.js

I'm working on a simple login site and to navigate between Login and Sign up site I made a state called "aktivSide" meaning "activeSite" which is supposed to determine whether the login site or the sign up site will display.
Whenever I click on the "Registrer Bruker" (sign up) button, I get this error message:
Uncaught TypeError: Cannot read properties of undefined (reading 'setState')
import planB from "./plan_b.json"
import logo from './bilder/logo.png'
import './App.css';
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome'
import { faEyeSlash, faEye } from '#fortawesome/free-solid-svg-icons'
import React from 'react';
class App extends React.Component {
//React
constructor(props) {
super(props);
this.state = {
aktivSide: "tom"
};
}
render() {
//JavaScript
return (
//jsx (javascript og HTML)
<>
<div onLoad={() => this.setState({ aktivSide: <LoggInn /> })} className="App">
<title>SpeedType</title>
<header className="App-header">
<div id="topp">
<img id="logoen" width={300} src={logo}></img>
</div>
<button onClick={() => console.log(this.state.aktivSide)}>state</button>
{this.state.aktivSide}
</header>
</div>
</>
)
function RegistrerBruker() {
return (
<>
<h1>Registrer Bruker</h1>
<input type="text" placeholder='brukernavn' className='input_text'></input>
<div className='passord'>
<input type="password" placeholder='passord' className='input_text'></input>
<FontAwesomeIcon className='eye_icon' id='eye_icon1' />
</div>
<p id='passordTekst'>tom</p>
<div className='knapper'>
<button className='knapper_login'><p>Logg Inn</p></button>
<button className='knapper_login'><p>Registrer deg</p></button>
</div>
</>
)
}
function LoggInn() {
//JavaScript
return (
//jsx
<>
<h1>Logg inn</h1>
<input type="text" placeholder='brukernavn' className='input_text'></input>
<div className='passord'>
<input type="password" placeholder='passord' className='input_text'></input>
<FontAwesomeIcon className='eye_icon' id='eye_icon1' />
</div>
<p id='passordTekst'>tom</p>
<div className='knapper'>
<button className='knapper_login'><p>Logg Inn</p></button>
<button onClick={() => this.setState({ aktivSide: <RegistrerBruker /> })} className='knapper_login'><p>Registrer deg</p></button>
</div>
</>
)
}
}
}
export default App;
I've tried adding:
this.aktivSide.bind(this);
to the constructor, but nothing will display.

Related

Using the useContext , it says object is not iterable (cannot read property Symbol(Symbol.iterator))?

When ever i am using useContext in the js file, it gives the error "TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator))". Removing that line , it works perfectly fine . I am not able to figure out the problem with this. Any help would be appreciated.
import { useRef } from "react";
import "./login.css";
import { loginCall } from "../../apicalls";
import { useContext } from "react";
import {AuthContext} from '../../context/AuthContext.js'
export default function Login() {
const email = useRef();
const password = useRef();
const [user,dispatch] = useContext(AuthContext);
const handleClick = (e) => {
e.preventDefault();
// loginCall(
// { email: email.current.value, password: password.current.value },
// dispatch
// );
console.log('hi');
};
// console.log(user);
return (
<div className="login">
<div className="loginWrapper">
<div className="loginLeft">
<h3 className="loginLogo">SocioFolio</h3>
<span className="loginDesc">
Connect with friends and the world around you on SocioFolio.
</span>
</div>
<div className="loginRight">
<form action=""
onSubmit={handleClick}
>
<div className="loginBox">
<input placeholder="Email" type="email" required className="loginInput" />
<input placeholder="Password" type="password" required minLength="6" className="loginInput" />
<button className="loginButton">Log In</button>
<span className="loginForgot">Forgot Password?</span>
<button className="loginRegisterButton">
Create a New Account
</button>
</div>
</form>
</div>
</div>
</div>
);
}

"Gets unauthorized error after submitting correct token"

I just try to hit the API when like and dislike button is clicked , but I get unauthorized error after passing the authorization header.
What is the solution of this problem. There is post where all users can like or dislike. There is only unauthorized error, everything is true.
This is the screenshot of where the action is written.
This is the screenshot where the error is displayed.
`JavaScript code
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import {connect} from 'react-redux'
import {Link} from 'react-router-dom';
import { deltePost, addLike, deleteLike } from
class PostItem extends Component {
onDelteClick(postId)
{
this.props.deltePost(postId);
}
onLikeClick(id)
{
this.props.addLike(id);
}
onunLikeClick(id)
{
this.props.deleteLike(id);
}
render() {
const {post,auth,showActions}=this.props;
return (
<section className="container">
<div className="posts">
<div className="post bg-white p-1 my-1">
<div>
<a href="profile.html">
<img
className="round-img"
src="//www.gravatar.com/avatar/05434e5d678bc30625550497804f6d0e?s=200&r=pg&d=mm"
alt=""
/>
<h4>{post.name}</h4>
</a>
</div>
<div>
<p className="my-1">
{post.text}
</p>
<p className="post-date">
{post.date}
</p>
{showActions ?(<span>
<button onClick={this.onLikeClick.bind(this, post._id)} type="button" className="btn btn-light">
<i className="fas fa-thumbs-up"></i>
<span>{post.likes.length}</span>
</button>
<button onClick={this.onunLikeClick.bind(this, post._id)} type="button" className="btn btn-light">
<i className="fas fa-thumbs-down"></i>
</button>
<Link to={`/post/${post._id}`} className="btn btn-primary">
Comments <span className='comment-count'>{post.comments.length}</span>
</Link>
{post.user === auth.user.id ?(
<button
type="button"
onClick={this.onDelteClick.bind(this,post._id)}
className="btn btn-danger"
>
<i className="fas fa-times"></i>
</button>
):null}
</span>) :null}
</div>
</div></div>
</section>
)
}
}
PostItem.defaultProps={
showActions:true
}
PostItem.propTypes={
deltePost:PropTypes.func.isRequired,
deleteLike:PropTypes.func.isRequired,
addLike:PropTypes.func.isRequired,
post:PropTypes.object.isRequired,
auth:PropTypes.object.isRequired
}
const mapStateToProps= state =>({
auth:state.auth
})
export default connect(mapStateToProps,{deltePost,addLike,deleteLike})(PostItem)
`

react Js - × TypeError: Cannot read properties of undefined (reading 'params'

Here im trying to edit a post on my web app. But when I it says ×
TypeError: Cannot read properties of undefined (reading 'params'
can someone point out the error. im doing this wacthing a tutorial. It works fine. but in my code it doesnt work. and I tried importing useParams and using it. but it says useParams cannot use in class components.
can someone point out the error or give me a fix please?
import React, {Component} from 'react';
import axios from 'axios';
import {Dropdown} from "react-bootstrap";
import {useParams} from "react-router-dom";
class Feed2 extends Component {
constructor(props){
super(props);
this.state={
vehicle:"",
plateNo:"",
owner:"",
manufacturer:"",
manufacturedYear:"",
color:""
}
}
takInput = (e) => {
const {name,value}= e.target;
this.setState({
...this.state,
[name]:value
})
}
register = (e) =>{
e.preventDefault();
const id = this.props.match.params.id;
const {plateNo,owner,manufacturer,manufacturedYear,color,vehicle} = this.state;
const data ={
plateNo:plateNo,
owner:owner,
manufacturer:manufacturer,
manufacturedYear:manufacturedYear,
color:color,
vehicle:vehicle
}
console.log(data);
axios.put(`http://localhost:8080/registrations/update/${id}`,data).then((res)=>{
if(res.data.success){
alert("Post Updated");
this.setState({
vehicle:"",
plateNo:"",
owner:"",
manufacturer:"",
manufacturedYear:"",
color:""
})
}
})
}
componentDidMount(){
const id = this.props.match.params.id;
axios.get(`/registrations/${id}`).then((res) =>{
if(res.data.success){
this.setState({
plateNo:res.data.registrations.plateNo,
owner:res.data.registrations.owner,
manufacturer:res.data.registrations.manufacturer,
manufacturedYear:res.data.registrations.manufacturedYear,
color:res.data.registration.color,
vehicle:res.data.registrations.vehicle
})
console.log(this.state.registrations)
}
})
}
render() {
return (
<div className="container">
<div className="register-box">
<div className="Top">
<div className="box">
<input placeholder="Enter the licence plate number "
className="input2" name="plateNo" value={this.state.plateNo} onChange={this.takInput} />
</div>
</div>
<div className="box">
<input placeholder="Enter the owners name "
className="input2" name="owner" value={this.state.owner} onChange={this.takInput} />
</div>
<div className="box">
<input placeholder=" Manufactured Year "
className="input2" name="manufacturedYear" value={this.state.manufacturedYear} onChange={this.takInput} />
</div>
<div className="box">
<input placeholder="Manufacturer"
className="input2" name="manufacturer" value={this.state.manufacturer} onChange={this.takInput} />
</div>
<div className="box">
<input placeholder="Color of the vehicle "
className="input2" name="color" value={this.state.color} onChange={this.takInput} />
</div>
<div className="box">
<input placeholder="Vehicle name "
className="input2" name="vehicle" value={this.state.vehicle} onChange={this.takInput} />
</div>
<Dropdown className="drop">
<Dropdown.Toggle variant="success" id="dropdown-basic">
Select Vehicle Type
</Dropdown.Toggle>
<Dropdown.Menu>
<Dropdown.Item href="#/action-1">Car</Dropdown.Item>
<Dropdown.Item href="#/action-2">Van</Dropdown.Item>
<Dropdown.Item href="#/action-3">Bus</Dropdown.Item>
<Dropdown.Item href="#/action-3">Lorry</Dropdown.Item>
<Dropdown.Item href="#/action-3">Three-weeler</Dropdown.Item>
<Dropdown.Item href="#/action-3">Bike</Dropdown.Item>
<Dropdown.Item href="#/action-3">Other</Dropdown.Item>
</Dropdown.Menu>
</Dropdown>
<div className="regbuttonContainer">
<button className="Button" onClick={this.register}>Register Vehicle</button>
</div>
</div>
</div>
);
}
}
export default Feed2;
I guess you need to wrap this component with withRouter HOC,
So it be like
export default withRouter(Feed2);
and import withRouter from 'react-router-dom';

render table via reactjs keep showing no matching records found

I'm using bootrap-table from http://bootstrap-table.wenzhixin.net.cn/.
I'm using reactjs to render the table after searching for data, the problem is although the table have data on it, it's still showing that "no matching
records found". Please help me, thank you.
This is my react code for render table:
import React from 'react';
import ReactDOM from 'react-dom';
import axios from 'axios';
class Form extends React.Component {
render() {
return (
<form onSubmit={this.props.submitData}>
<h2 className="card-inside-title">Floating Label Examples</h2>
<div className="row clearfix">
<div className="col-sm-12">
<div className="form-group form-float form-group-lg">
<div className="form-line">
<input type="text" className="form-control" name="stockNumber" autoFocus
onChange={this.props.inputChange}/>
<label className="form-label">Stock number</label>
</div>
</div>
<div className="form-group form-float form-group-lg">
<div className="form-line">
<input type="text" className="form-control" name="personalPassportId"
onChange={this.props.inputChange}/>
<label className="form-label">Personal ID/Passport number</label>
</div>
</div>
<div className="form-group form-float form-group-lg">
<div className="form-line">
<input type="text" className="form-control" name="name"
onChange={this.props.inputChange}/>
<label className="form-label">Stockholder name</label>
</div>
</div>
</div>
</div>
<div className="row clearfix">
<div className="col-sm-12">
<button type="submit" className="btn btn-lg btn-primary waves-effect">
Submit
</button>
</div>
</div>
</form>
)
}
}
class Rows extends React.Component {
render() {
return (
<tr>
<td>{this.props.rowData.stocknumber}</td>
<td>{this.props.rowData.personalpassportid}</td>
<td>{this.props.rowData.name}</td>
</tr>
)
}
}
class Table extends React.Component {
constructor(props) {
super(props);
this.state = {
rowsData: []
};
}
componentWillReceiveProps(nextProps) {
this.setState({
rowsData: nextProps.tableData
});
}
render() {
var rows = [];
this.state.rowsData.forEach((rowsData) => {
rows.push(<Rows rowData={rowsData} key={rowsData.id}/>)
});
return (
<table data-toggle="table">
<thead className="bg-deep-orange">
<tr>
<th>Stock Number</th>
<th>Personal/Passport ID</th>
<th>Name</th>
</tr>
</thead>
<tbody>{rows}</tbody>
</table>
)
}
}
class Content extends React.Component {
constructor(props) {
super(props);
this.state = {
stockNumber: '',
personalPassportId: '',
name: '',
tableData: null
};
this.inputChange = this.inputChange.bind(this);
this.submitData = this.submitData.bind(this)
};
inputChange(event) {
const target = event.target;
const value = target.value;
const name = target.name;
this.setState({
[name]: value
});
};
submitData(event) {
const {stockNumber, personalPassportId, name} = this.state;
axios.post('/attend', {
stockNumber: stockNumber,
personalPassportId: personalPassportId,
name: name
}).then((response) => {
this.setState({
tableData: response.data
});
}).catch((err) => {
console.log(err);
});
event.preventDefault();
};
render() {
return (
<div>
<Form inputChange={this.inputChange} submitData={this.submitData}/>
<div className="row clearfix">
<div className="col-sm-12">
<Table tableData={this.state.tableData}/>
</div>
</div>
</div>
)
}
}
ReactDOM.render(<Content/>, document.getElementById('content'));
Table keep showing "No matching records found"

ReduxForm handleSubmit refreshes page with fields assigned

Environment
ReduxForm: v6.5.0
Node: v8.1.2
Browser: Google Chrome
I've gone through all the existing issues on handleSubmit page refreshing, but none of them seems to solve my problem.
LoginForm
import React, { Component } from 'react'
import { reduxForm, Field, propTypes } from 'redux-form'
import classNames from 'classnames'
import loginValidation from './validation'
#reduxForm({
form: 'loginForm',
validate: loginValidation
})
export default class LoginForm extends Component {
static propTypes = {
...propTypes
}
inputField = ({ input, label, type, meta: { touched, error } }) => (
<fieldset className="form__fieldset login-form__fieldset">
<div className="form__field">
<input {...input}
type={type}
placeholder={touched && error ? error : label}
className={classNames('form__input login-form__input',
touched && error ? 'ng-invalid' : ''
)}
/> {/* .ng-invalid */}
</div>
{type === 'password' &&
<div className="form__helper login-form__helper">
<div className="small">
<a>I've forgotten my password</a>
</div>
</div>
}
</fieldset>
)
render() {
const { inputField, props } = this
const { handleSubmit, submitting } = props
return (
<div className="habbo-login-form">
<form className="login-form__form" onSubmit={handleSubmit}>
<Field name="username" type="text" component={inputField} label="Username" />
<Field name="password" type="password" component={inputField} label="Password" />
<button className="login-form__button" type="submit" disabled={submitting}>Let's go!</button>
</form>
<div className="login-form__social">
<div className="habbo-facebook-connect" type="large">
<button className="facebook-connect">Login with Facebook</button>
</div>
<div className="habbo-facebook-connect" type="small">
<button className="facebook-connect"></button>
</div>
</div>
</div>
)
}
}
And the HOC LoginHeader:
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import classNames from 'classnames'
import LoginForm from './LoginForm'
import { authActions } from '../redux/modules/auth'
#connect(
state => ({
user: state.auth.user,
...state.form.loginForm
}),
{ ...authActions }
)
export default class LoginHeader extends Component {
static propTypes = {
user: PropTypes.object,
login: PropTypes.func.isRequired,
logout: PropTypes.func.isRequired
}
static contextTypes = {
router: PropTypes.object
}
onSubmit = (data) => this.props.login(data).then(console.log)
render() {
const { submitFailed } = this.props
const headerLoginForm = classNames(
'header__login-form',
submitFailed ? 'animated shake' : ''
)
return (
<div className="header__top sticky-header sticky-header--top">
<div className="wrapper">
<div className="header__top__content">
<div className={headerLoginForm}>
<LoginForm onSubmit={this.onSubmit} />
</div>
</div>
</div>
</div>
)
}
}
Even when I try to replace <form onSubmit={handleSubmit}> with <form onSubmit={(e) => e.preventDefault()}> my page still refreshes.
I'm unsure whether or not this is a problem with my coding, browser, version, or whatever else because practically yesterday it worked without any problem.

Resources