【发布时间】:2019-07-06 04:11:33
【问题描述】:
我正在使用 react 和 react-apollo 客户端。
这是我的主要路线文件
const PrivateRoute = ({ component, isAuthed, ...rest }) => {
console.log({ isAuthed })
return (
<Route {...rest} exact
render = {(props) => (
isAuthed ? (
<div>
{React.createElement(component, props)}
</div>
) :
(
<Redirect
to={{
pathname: '/login',
state: { from: props.location }
}}
/>
)
)}
/>
)
}
class App extends Component {
state = {
isAuthed: false
}
async componentWillMount() {
this.setState({ isAuthed: true })
}
render() {
const { isAuthed } = this.state
return (
<div style={{ direction: direction }}>
<Header {...this.props} history={history}/>
<Router history={history}>
<Switch>
<Route exact path="/" render={() => <Redirect to="/dashboard" />} />
<Route path="/login" component={Login}/>
<PrivateRoute isAuthed={isAuthed} path="/dashboard" component={Dashboard} />
<PrivateRoute isAuthed={isAuthed} path="/AdminManagement" component={Admin} />
</Switch>
</Router>
</div>
)
}
}
export default compose(
graphql(SET_SESSION, { name: 'setSession' })
)((withNamespaces)('common')(App))
现在,当我在登录组件中登录时,我需要将 isAuthed 设置为 true,这在我的主路由文件中(上一个)
如何使用 react apollo 客户端做到这一点?
【问题讨论】:
-
这个问题缺少很多细节......现在没有人可以帮助你。什么查询?
Login是什么?为什么是阿波罗客户端?你的后端怎么样? -
@Treycos 不要太深入。只是想知道如何在多个组件中更新/获取相同的
prop,就像我们对 react redux 所做的那样
标签: reactjs ecmascript-6 graphql react-apollo apollo-client