Reactjs React中组件之间的传递状态
这可能是一个相当容易的解决办法,但我是一个反应迟钝的人,我不太明白。我有一个SignIn组件,它可以跟踪用户登录时的状态。我想将此状态传递给仪表板组件,以显示一些用户信息 SignIn.js:Reactjs React中组件之间的传递状态,reactjs,Reactjs,这可能是一个相当容易的解决办法,但我是一个反应迟钝的人,我不太明白。我有一个SignIn组件,它可以跟踪用户登录时的状态。我想将此状态传递给仪表板组件,以显示一些用户信息 SignIn.js: import React, { Component } from 'react' import firebase from '../config/firebaseConfig' import { Redirect } from 'react-router-dom' import GoogleLoginB
import React, { Component } from 'react'
import firebase from '../config/firebaseConfig'
import { Redirect } from 'react-router-dom'
import GoogleLoginButton from './GoogleLoginButton'
class SignIn extends Component {
state = {
email: '',
password: '',
user: null
}
handleChange = (e) => {
this.setState({
[e.target.id]: e.target.value
})
}
signIn = (e) => {
e.preventDefault()
firebase.auth().signInWithEmailAndPassword(this.state.email, this.state.password)
.then(user => {
this.setState({
user
})
})
.catch(err => {
console.log(err)
})
}
signUp = () => {
this.props.history.push('/signup')
}
render() {
if (this.state.user) return <Redirect to='/' />
return (
<div className="container">
<div>
<form id="signIn">
<div className="input-field">
<label htmlFor="email">Email</label>
<input type="email" id="email" onChange={this.handleChange} />
</div>
<div className="input-field">
<label htmlFor="password">Password</label>
<input type="password" id="password" onChange={this.handleChange} />
</div>
<div className="input-field">
<button className="btn blue left" onClick={this.signIn}>Sign In</button>
</div>
</form>
<div>
<button className="btn blue right" onClick={this.signUp}>Sign Up</button>
</div>
</div>
<br/><br/><br/>
<div className="center">
<GoogleLoginButton />
</div>
</div>
)
}
}
export default SignIn
import React, { Component } from 'react'
import SignOutButton from './SignOutButton'
class Dashboard extends Component {
render() {
return (
<div className="container center">
<div>
<SignOutButton />
</div>
</div>
)
}
}
export default Dashboard
import React,{Component}来自“React”
从“../config/firebaseConfig”导入firebase
从“react router dom”导入{Redirect}
从“./GoogleLoginButton”导入GoogleLoginButton
类签名扩展组件{
状态={
电子邮件:“”,
密码:“”,
用户:空
}
handleChange=(e)=>{
这是我的国家({
[e.target.id]:e.target.value
})
}
签名=(e)=>{
e、 预防默认值()
firebase.auth().signiWithEmailandPassword(this.state.email,this.state.password)
。然后(用户=>{
这是我的国家({
用户
})
})
.catch(错误=>{
console.log(错误)
})
}
注册=()=>{
this.props.history.push(“/signup”)
}
render(){
如果(this.state.user)返回
返回(
电子邮件
密码
登录
注册
)
}
}
导出默认签名
Dashboard.js:
import React, { Component } from 'react'
import firebase from '../config/firebaseConfig'
import { Redirect } from 'react-router-dom'
import GoogleLoginButton from './GoogleLoginButton'
class SignIn extends Component {
state = {
email: '',
password: '',
user: null
}
handleChange = (e) => {
this.setState({
[e.target.id]: e.target.value
})
}
signIn = (e) => {
e.preventDefault()
firebase.auth().signInWithEmailAndPassword(this.state.email, this.state.password)
.then(user => {
this.setState({
user
})
})
.catch(err => {
console.log(err)
})
}
signUp = () => {
this.props.history.push('/signup')
}
render() {
if (this.state.user) return <Redirect to='/' />
return (
<div className="container">
<div>
<form id="signIn">
<div className="input-field">
<label htmlFor="email">Email</label>
<input type="email" id="email" onChange={this.handleChange} />
</div>
<div className="input-field">
<label htmlFor="password">Password</label>
<input type="password" id="password" onChange={this.handleChange} />
</div>
<div className="input-field">
<button className="btn blue left" onClick={this.signIn}>Sign In</button>
</div>
</form>
<div>
<button className="btn blue right" onClick={this.signUp}>Sign Up</button>
</div>
</div>
<br/><br/><br/>
<div className="center">
<GoogleLoginButton />
</div>
</div>
)
}
}
export default SignIn
import React, { Component } from 'react'
import SignOutButton from './SignOutButton'
class Dashboard extends Component {
render() {
return (
<div className="container center">
<div>
<SignOutButton />
</div>
</div>
)
}
}
export default Dashboard
import React,{Component}来自“React”
从“./signburatton”导入signburatton
类仪表板扩展组件{
render(){
返回(
)
}
}
导出默认仪表板
将登录状态传递给仪表板组件的最佳方式是什么?对于使用当前用户,您需要将其保持在您的状态,以便跨应用程序使用 使用选项 1-上下文api 2-重复 3-本地存储如下所示:
<Redirect to={{
pathname: '/',
state: { id: 1 }
}}
/>
您还可以在SignIN中传递来自状态的任何数据
<Redirect to={{
pathname: '/',
state: { email: this.state.email }
}}
/>
您还可以执行以下操作:
this.props.history.push("/", { response: YOUR_DATA }) FROM signin
并在仪表板中访问它,如:
const state = this.props.location.state
我建议有三种方法
<LoginForm setUser={user => this.setState({user})}/>
this.setState({user})}/>
并在另一个组件(如Dashboard)的父组件中使用此状态
您可以使用react路由器在不同的路由之间传递数据,如下所示
this.props.router.push({
pathname: '/newPage',
state: {
newValue: 123
}
})
或者其他方法是使用cookie或本地存储
import Cookies from 'universal-cookie';
const cookies = new Cookies();
export const SESSION_USER_NAME = 'SESSION_USER_NAME';
cookies.set(SESSION_USER_NAME, userName);
and to get value from cookie
cookies.get(SESSION_USER_NAME),