我react-relay
在尝试实例化扩展类时遇到了一个非常奇怪的 TypeError Relay.Mutation
。我创建了一个简单的 Typescript 项目create-react-app my-app --scripts-version=react-scripts-ts
来演示这个问题。
每当我跑步yarn start
时,这就是我所看到的:
这个错误对我来说真的没有意义:
TypeError: Object prototype may only be an Object or null: undefined
我只是想实例化我自己的扩展Relay.Mutation
类的一个新实例。总的来说,我对 React Relay 世界和 ES6/Typescript 真的很陌生,所以我可能只是想念一些愚蠢的东西。
在这个简单的项目中,我直接使用了DefineTyped repository中定义的示例类。
我不应该像这样使用这个类吗:
const atm = new AddTweetMutation({ text: 'asdf', userId: '1123' });
下面是这个 AddTweetMutation.tsx
类的样子:
import * as Relay from 'react-relay';
interface Props {
text: string;
userId: string;
}
interface State {
}
export default class AddTweetMutation extends Relay.Mutation<Props, State> {
public getMutation() {
return Relay.QL`mutation{addTweet}`;
}
public getFatQuery() {
return Relay.QL`
fragment on AddTweetPayload {
tweetEdge
user
}
`;
}
public getConfigs() {
return [{
type: 'RANGE_ADD',
parentName: 'user',
parentID: this.props.userId,
connectionName: 'tweets',
edgeName: 'tweetEdge',
rangeBehaviors: {
'': 'append',
},
}];
}
public getVariables() {
return this.props;
}
}
这是整个Hello.tsx
React 组件:
import * as React from 'react';
import AddTweetMutation from '../mutations/AddTweetMutation';
export interface Props {
name: string;
enthusiasmLevel?: number;
}
class Hello extends React.Component<Props, {}> {
render() {
const atm = new AddTweetMutation({ text: 'asdf', userId: '1123' });
console.log(atm);
const { name, enthusiasmLevel = 1 } = this.props;
if (enthusiasmLevel <= 0) {
throw new Error('You could be a little more enthusiastic. :D');
}
return (
<div className="hello">
<div className="greeting">
Hello {name + getExclamationMarks(enthusiasmLevel)}
</div>
</div>
);
}
}
export default Hello;
// helpers
function getExclamationMarks(numChars: number) {
return Array(numChars + 1).join('!');
}
这就是我的package.json
样子:
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@types/jest": "^20.0.6",
"@types/node": "^8.0.19",
"@types/react": "^16.0.0",
"@types/react-dom": "^15.5.2",
"@types/react-relay": "^0.9.13",
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-relay": "^1.1.0",
"react-scripts-ts": "2.5.0"
},
"devDependencies": {},
"scripts": {
"start": "react-scripts-ts start",
"build": "react-scripts-ts build",
"test": "react-scripts-ts test --env=jsdom",
"eject": "react-scripts-ts eject"
}
}
更新:输入当前对 react-relay > 1.x 无效。有关此问题,请参阅github 上的线程。我还使用解决方法更新了我的存储库。