Relay 使用路由将入口定义为 Relay 应用程序。
附注
Relay 路由并不真正实现任何URL路由特定的逻辑或使用历史 API。在将来,我们可能会将RelayRoute重命名为更多类似于RelayQueryRoots或RelayQueryConfig的内容。
属性
方法
static paramDefinitions: {[param: string]: {required: boolean}}
路由可以声明一组需要提供给构造函数的参数名称。这也是记录一组有效参数的一个方便的地方。
class ProfileRoute extends Relay.Route { static paramDefinitions = { userID: {required: true}, }; // ... }
static prepareParams: ?(prevParams: {[prevParam: string]: mixed}) => {[param: string]: mixed};
路由可用于 prepareParams
提供默认参数 或通过 convert或
suppress 传入参数。
class ProfileRoute extends Relay.Route { static queries = { viewer: () => Relay.QL`query { viewer }` }; static prepareParams = (prevParams) => { return { // Pass base set of supplied params through: ...prevParams, // Transform a param to meet internal requirements: id: toGlobalId('Profile', prevParams.id), // Provide a starting `limit` variable: limit: 10, } } // ... }
static queries: { [queryName: string]: () => Relay.QL`query { ... }` };
路由必须声明一组根查询 Relay.QL
。 这些查询将自动
在 Relay.RootContainer 上组成与此路由一起使用的Relay 容器上命名的匹配片段。
class ProfileRoute extends Relay.Route { static queries = { user: () => Relay.QL`query { user(id: $userID) }`, }; // ... }
在此示例中,Route应该被初始化为一个 userID
传递给查询的路由。 userID
变量将自动传递到顶级容器,如果需要可以在那里使用。此外,顶级RelayContainer预计将具有 user
要查询字段的片段。
static routeName: string
路由必须定义一个字符串名称。
使用 new
关键字创建路由实例,可选地传递一些参数。
var profileRoute = new ProfileRoute({userID: '123'});