Reactjs 如何访问组件外部的React上下文?
我正在尝试实现一个函数,该函数通过一个按钮从功能组件中调用 它应该从我自己的数据库中删除一个用户。但是我需要Firebase的访问令牌来对我的后端进行这个受保护的API调用 现在我从上下文API为firebase实例提供服务,但我似乎无法找到从React组件外部访问该实例的方法 我得到了这个错误: 第10行:应为赋值或函数调用,而不是看到表达式 我是不是走错了路Reactjs 如何访问组件外部的React上下文?,reactjs,Reactjs,我正在尝试实现一个函数,该函数通过一个按钮从功能组件中调用 它应该从我自己的数据库中删除一个用户。但是我需要Firebase的访问令牌来对我的后端进行这个受保护的API调用 现在我从上下文API为firebase实例提供服务,但我似乎无法找到从React组件外部访问该实例的方法 我得到了这个错误: 第10行:应为赋值或函数调用,而不是看到表达式 我是不是走错了路 import React from 'react'; import axios from 'axios'; import { Pas
import React from 'react';
import axios from 'axios';
import { PasswordForgetForm } from '../PasswordForgetForm/PasswordForgetForm';
import PasswordChangeForm from '../PasswordChangeForm/PasswordChangeForm';
import { AuthUserContext, withAuthorization } from '../../services/Session';
import { FirebaseContext } from '../../services/Firebase';
const deletUser = (authUser) => {
{
firebase => {
const token = firebase.doGetIdToken();
console.log(token);
axios.delete('/api/users/' + authUser.uid, {
headers: {
authorization: `Bearer ${token}`
}
})
.then(res => {
//this.props.history.push('/dashboard');
console.log(res);
})
}
}
}
const AccountPage = () => (
<AuthUserContext.Consumer>
{authUser => (
<div>
<h1>Account: {authUser.email}</h1>
<PasswordForgetForm />
<PasswordChangeForm />
<button type="button" onClick={() => deletUser(authUser)}>Delete Account</button>
</div>
)}
</AuthUserContext.Consumer>
);
const condition = authUser => !!authUser;
export default withAuthorization(condition)(AccountPage);
从“React”导入React;
从“axios”导入axios;
从“../PasswordForgetForm/PasswordForgetForm”导入{PasswordForgetForm};
从“../PasswordChangeForm/PasswordChangeForm”导入PasswordChangeForm;
从“../../services/Session”导入{AuthUserContext,withAuthorization};
从“../../services/Firebase”导入{FirebaseContext};
const deletUser=(authUser)=>{
{
firebase=>{
const token=firebase.doGetIdToken();
console.log(令牌);
删除('/api/users/'+authUser.uid{
标题:{
授权:`Bearer${token}`
}
})
。然后(res=>{
//this.props.history.push('/dashboard');
控制台日志(res);
})
}
}
}
const AccountPage=()=>(
{authUser=>(
帐户:{authUser.email}
deletUser(authUser)}>删除帐户
)}
);
const condition=authUser=>!!authUser;
使用授权导出默认值(条件)(AccountPage);
谢谢你的帮助 代码声明的是匿名对象,内部语法不正确
const deletUser = (authUser) => {
{//anonymous object
firebase => {//There is no key for the member of the object
const token = firebase.doGetIdToken();
console.log(token);
axios.delete('/api/users/' + authUser.uid, {
headers: {
authorization: `Bearer ${token}`
}
})
.then(res => {
//this.props.history.push('/dashboard');
console.log(res);
})
}
}//You never call or return anything of your object
}
deletUser
的可能副本并不是真正调用任何函数,而是声明了一个函数,该函数采用firebase InstanceId您的意思是在deletUser
中有两组打开和关闭花括号?(即constdeletuser=(authUser)=>{{firebase=>..…}}
)另外,我不熟悉constcondition=authUser=>!!authUser
语法。这不就是把它本身设置为“不是”吗?