我正在尝试创建一个在加载 DOM 时直接执行的组件 onInit();
此函数将 token 发布到端点,如果成功,我将尝试运行一个名为“valid()”的函数
我不断遇到的问题是,当我尝试调用“有效”函数作为响应时,它说不能历史未定义。
我认为我没有以正确的方式传递 Prop 。
此外,如果不成功,应返回错误页面。
感谢您对此的任何帮助
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>
);
}
}
最佳答案
您没有向 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
关于javascript - React中如何在函数中调用函数(auth、路由),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61395761/