admin管理员组文章数量:1389768
If a component has a lot of asynchronously-loaded data, I often find myself writing something like this:
interface MyState {
summary: string | null;
leftContents: string | null;
rightContents: string | null;
number: number | null;
}
interface MyProps {
id: number,
}
class MyComponent extends React.Component<MyProps, MyState> {
state: MyState = {
summary: null,
leftContents: null,
rightContents: null,
number: null,
}
componentDidMount() {
fetch(`/api/${this.props.id}/summary`)
.then(res => res.json())
.then(data => {
this.setState({
summary: data['summary']
});
});
fetch(`/api/${this.props.id}/leftContents`)
.then(res => res.json())
.then(data => {
this.setState({
leftContents: data['leftContents']
});
});
...
And then the rest of the component is littered with this.state.summary === null ?
checks and this.state.summary?.includes
optional chains etc.
Inspired by seeing TypeState patterns in Rust, I would rather write code where it is illegal to access this.state.summary
until the summary has finished loading and been set, and the compiler will be able to tell me and help me.
I imagine this should be possible with types, but I haven't been able to yet think of a way to solve it, because the component only has one MyState
generic parameter.
How can I reflect the current runtime state of the component in the type system, so that the compiler can help me and so that I won't have to have null checks everywhere?
If a component has a lot of asynchronously-loaded data, I often find myself writing something like this:
interface MyState {
summary: string | null;
leftContents: string | null;
rightContents: string | null;
number: number | null;
}
interface MyProps {
id: number,
}
class MyComponent extends React.Component<MyProps, MyState> {
state: MyState = {
summary: null,
leftContents: null,
rightContents: null,
number: null,
}
componentDidMount() {
fetch(`/api/${this.props.id}/summary`)
.then(res => res.json())
.then(data => {
this.setState({
summary: data['summary']
});
});
fetch(`/api/${this.props.id}/leftContents`)
.then(res => res.json())
.then(data => {
this.setState({
leftContents: data['leftContents']
});
});
...
And then the rest of the component is littered with this.state.summary === null ?
checks and this.state.summary?.includes
optional chains etc.
Inspired by seeing TypeState patterns in Rust, I would rather write code where it is illegal to access this.state.summary
until the summary has finished loading and been set, and the compiler will be able to tell me and help me.
I imagine this should be possible with types, but I haven't been able to yet think of a way to solve it, because the component only has one MyState
generic parameter.
How can I reflect the current runtime state of the component in the type system, so that the compiler can help me and so that I won't have to have null checks everywhere?
Share Improve this question asked Mar 15 at 4:22 NilsNils 3301 silver badge10 bronze badges 1- 1 I edit the question with working completed code using jsonplaceholder APIs. It will load the relevant data for passing prop id value. – Shashika Silva Commented Mar 15 at 6:20
1 Answer
Reset to default 1A couple of thoughts here:
- Use child component with props defined like so:
interface ISummaryProps {
summary: string
}
Render child component in the parent only when parent finished fetching (conditional rendering). This way you keep business logic in the parent and rendering logic in children.
- Create getter in your component like so:
private get summary(){
if(!this.state.summary) {
throw new Error('Expected summary but got undefined')
}
return this.state.summary
}
After this you can just use this.summary
and it will return string
but you need to make sure you don't attempt to access the field before fetching is done.
本文标签: reactjsTypestate pattern in ReactStack Overflow
版权声明:本文标题:reactjs - Typestate pattern in React - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744624092a2616210.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论