简体   繁体   English

如何在 React 中将状态从组件传递到文件 app.js

[英]How to pass state from component to file app.js in React

I have to pass my state from Authentication.js to App.js, to make certain functionalities available to registered users.我必须将我的状态从 Authentication.js 传递到 App.js,以使某些功能可供注册用户使用。

This is my file Authentication.js这是我的文件 Authentication.js

import { useState } from "react"
import MyButton from "../componens/myButton"
import styles from "../style/authentication.module.css"
import Spinner from "../componens/Spinner"
import axios from "axios"

const Authentication = () =>{

const [email, setEmail] = useState("")
const [password, setPassword] = useState("")
const [loading, setLoading] = useState(false)
const [registration, setRegistration] = useState(true)
const [state, setState] = useState(null)

let MyRegistration = null;

const handleSubmit = async (e) =>{
    e.preventDefault()
    setLoading(true)
    try {
        const userKey = `AIzaSyCYe1XVuJVflRie0sf1y01RNrmQ77Dmp4Q`
        let urlAPI = `https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=${userKey}`
        if (!registration) {
            urlAPI = `https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=${userKey}`
        }
        setEmail(email);
        setPassword(password);
        MyRegistration = await axios.post(urlAPI,{
            email : email,
            password : password,
            returnSecureToken : true
        })
        setState(MyRegistration)
        setLoading(false);

        return MyRegistration

    } catch (error) {
        setLoading(false)
        console.log(error)
    }
}
    return <div className={styles.formContainer}>
        <h1>Authentication</h1>
        <form onSubmit={handleSubmit} action="">
            <p>Email</p>
            <input placeholder="Your email" type="email" value={email} onChange={handleEmail} />
            <p>Password</p>
            <input placeholder="Your Password" type="password" value={password} onChange={handlePassword} />
            {loading ? <Spinner/> :
            <MyButton  title={registration ? 'sign up' : 'Sign in'} handleClickButton={handleSubmit} />}
            <MyButton title={registration ? 'Go to Sign in' : 'Go to sign up'} handleClickButton={changeMode}/>
        </form>
    </div>
}
export default Authentication;

This is my file App.js这是我的文件 App.js

import Home from './pages/Home';
import Book from './pages/Book'
import {Route, Switch} from 'react-router-dom'; 
import SavedBooks from './pages/SavedBooks';
import Header from './componens/Header';
import BookChapter from './pages/BookChapter';
import Authentication from './pages/Authentication';
import {useState} from "react";

function App(props) {

const [userData,  setUserData] = useState("");

  return (
    <div>
      <Header/>
      <Switch>
        <Route exact path="/" render={(props) => <Home userData={userData}/>}/>
        <Route exact path="/book/:id" component={Book}/>
        <Route exact path="/savedbooks" component={SavedBooks}/>
        <Route exact path="/book/:id/chapter/:numero" component={BookChapter}/>
        <Route exact path="/authentication" render={(props) => <Authentication setUserData={setUserData(this.state)}/> }/>
      </Switch>
    </div>
  )
}
export default App;

But the response is: TypeError: Cannot read properties of undefined (reading 'state')但响应是: TypeError: Cannot read properties of undefined (reading 'state')

You cannot pass the state declared in Authentication.js to its parent App.js .您不能将Authentication.js声明的状态传递给其父App.js So, you need to make a useState in App.js and pass the state and its corresponding setterFunction onto the child ie Authentication.js .所以,你需要在App.js创建一个 useState 并将状态和它对应的 setterFunction 传递给孩子,即Authentication.js This is known as lifting up the state.这被称为提升状态。

Move const [state, setState] = useState(null) to App.js and use the state variable in App.js and pass it onto child like thisconst [state, setState] = useState(null)App.js并使用App.jsstate变量并将其传递给 child 像这样

<Route exact path="/authentication" render={(props) => <Authentication state={state} setState={setState}/> }/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM