Javascript 如何在React(身份验证、路由)中的函数中调用函数
我试图创建一个在加载DOM时直接执行的组件,onInit() 此函数将令牌发布到端点,如果成功,我将尝试运行名为“valid()”的函数 我一直遇到的问题是,当我试图调用'valid'函数作为响应时,它说不能调用未定义的历史 我想我传递道具的方式不对 此外,如果失败,应返回错误页面 谢谢你在这方面的帮助Javascript 如何在React(身份验证、路由)中的函数中调用函数,javascript,reactjs,function,authentication,routes,Javascript,Reactjs,Function,Authentication,Routes,我试图创建一个在加载DOM时直接执行的组件,onInit() 此函数将令牌发布到端点,如果成功,我将尝试运行名为“valid()”的函数 我一直遇到的问题是,当我试图调用'valid'函数作为响应时,它说不能调用未定义的历史 我想我传递道具的方式不对 此外,如果失败,应返回错误页面 谢谢你在这方面的帮助 export class LandingPage extends Component { constructor(props) { super(props);
export class LandingPage extends Component {
constructor(props) {
super(props);
this.state = {};
this.valid = this.valid.bind(this);
}
valid = () => {
auth.login(() => {
this.props.history.push("/app");
});
};
componentDidMount() {
onInit();
function onInit(props) {
const apiUrl = "www.somefakedomain.com/endpoint"
axios
.post(apiUrl, {
token: 'somevalue123'
})
.then(function(response) {
console.log(response);
//CALL VALID FUNCTION HERE
this.valid; //throws error, how to run function here
})
.catch(function(error) {
console.log(error);
//Show Error Page
});
}
}
render() {
return (
<div>
<Spinner />
</div>
);
}
}
导出类登录页扩展组件{
建造师(道具){
超级(道具);
this.state={};
this.valid=this.valid.bind(this);
}
有效=()=>{
身份验证登录(()=>{
this.props.history.push(“/app”);
});
};
componentDidMount(){
onInit();
功能onInit(道具){
const apiUrl=“www.somefakedomain.com/endpoint”
axios
.post(apirl{
标记:“somevalue123”
})
.然后(功能(响应){
控制台日志(响应);
//在这里调用有效函数
this.valid;//抛出错误,如何在此处运行函数
})
.catch(函数(错误){
console.log(错误);
//显示错误页
});
}
}
render(){
返回(
);
}
}
您没有向onInIt函数传递任何内容
你是不是想做这样的事
export class LandingPage extends Component {
constructor(props) {
super(props);
this.state = {};
this.valid = this.valid.bind(this);
}
valid = () => {
auth.login(() => {
this.props.history.push("/app");
});
};
componentDidMount() {
function onInit(props) {
const apiUrl = "www.somefakedomain.com/endpoint"
axios
.post(apiUrl, {
token: 'somevalue123'
})
.then(function(response) {
console.log(response);
//CALL VALID FUNCTION HERE
this.valid(); //need to call function not reference it//throws error, how to run function here
})
.catch(function(error) {
console.log(error);
//Show Error Page
});
}
onInIt(this.props);
}
render() {
return (
<div>
<Spinner />
</div>
);
}
}
javascript reactjs function authent
导出类登录页扩展组件{
建造师(道具){
超级(道具);
this.state={};
this.valid=this.valid.bind(this);
}
有效=()=>{
身份验证登录(()=>{
this.props.history.push(“/app”);
});
};
componentDidMount(){
功能onInit(道具){
const apiUrl=“www.somefakedomain.com/endpoint”
axios
.post(apirl{
标记:“somevalue123”
})
.然后(功能(响应){
控制台日志(响应);
//在这里调用有效函数
this.valid();//需要调用函数而不是引用它//抛出错误,如何在此处运行函数
})
.catch(函数(错误){
console.log(错误);
//显示错误页
});
}
onInIt(this.props);
}
render(){
返回(
);
}
}
javascript函数authent
您没有使用参数调用函数