【发布时间】:2019-03-20 06:01:33
【问题描述】:
有没有办法在 React 组件中检索使用 Express JS render() API 发送的数据?
express 数据正在发送过来,但我唯一能捕捉到它的级别是在 pug 模板中,但我想在 React 组件中传输或捕捉它,以便我可以从组件内的数据库中检索更多数据。
主要问题是我要发送的数据来自passport,在检查用户是否正在通过身份验证之后。
这是特快路线
response.render(path.resolve('views', 'account.pug'), {user: request.user});
这是pug模板的一部分
body
#navbar
#account
我在其中渲染这个组件
class Account extends React.Component
{
constructor(props)
{
super(props);
}
componentWillMount()
{
console.log(this.props);
}
render()
{
return(
<div class='account'>
</div>
);
}
}
我想知道是否有一种优雅的方法可以做到这一点,无需 cookie 操作或其他解决方法。 如果不是,您认为将数据从后端发送到 React 组件的最佳方式是什么?
【问题讨论】:
-
为了做到这一点,你在前端使用 React 并在服务器上表达?
-
是的,当然。 Express 用于路由,React 用于呈现具有动态数据的页面。这就是为什么我需要来自 React 组件中 express API 的这些信息。