divanov11 / proshop_django

409 stars 434 forks source link

Cannot read property of undefined loginPage.js : 19 (reading 'search' ) #37

Open TheNexusDeveloper opened 2 years ago

TheNexusDeveloper commented 2 years ago

Screenshot 2022-11-10 110823

the code is shown below, I believe the error is occuring because I'm using react router v6 how do I write this code in react router v6

import React, {useState, useEffect} from 'react' import { Link } from 'react-router-dom' import { Form, Button, Row, Col } from 'react-bootstrap' import { useDispatch, useSelector } from 'react-redux' import Loader from '../components/Loader' import Message from '../components/Message' import FormContainer from '../components/FormContainer'

import { login } from '../actions/userActions'

function Loginpage({ Location, history }) { const [email, setEmail] = useState('') const [password, setPassword] = useState('')

const dispatch = useDispatch()

const redirect = Location.search ? Location.search.split('=')[1] : '/'

const userLogin = useSelector(state => state.userLogin)
const {error, loading, userInfo} = userLogin 

useEffect(() => {
    if (userInfo) {
        history.push(redirect)
    }
},  [history, userInfo, redirect])

const submitHandler = (e) =>{
    e.preventDefault()
    dispatch(login(email, password))
}

return (

Sign In

{error &&{error}} {loading && }
Email Address setEmail(e.target.value)} > Password setPassword(e.target.value)} >
Don't Have an account? > Register

) }

export default Loginpage

Cvija04 commented 1 year ago

Its about react-router-dom v6, you can't use location.search, import useSearchParamas in react-router-dom, but i didnt find yet how to use it, if you have error in LoginScreen, you need to have error with CartScreen, because it use same method

bbosajon commented 1 year ago

in react v6, u need use: useLocation from reactre-router-dom and remove the location parameter then it will work