由于ts的大潮势不可挡,最近的一个react项目开始使用ts来写,又由于之前都是用的redux作为状态管理,据说mobx和ts相性更好,就选择了这么个组合来开发新项目。期间遇到的问题稍微记录一下。
mobx中对象更新的问题
描述: 在store中定义一个对象
@observeble obj = {a:1};
然后定义一个方法来改变a的值
@action changeA = (value:string) => {
this.obj.a = value;
}
更改之后发现页面并没有接收到改变,
解决: 发现需要对obj重新赋值才行,遂改成了这样写
@action changeA = (value:string) => {
this.obj = {
…this.obj,
a:value;
}
}
类似的,数组也是,如果store中有一个数组arr,直接使用arr.push(a)之类的方法,页面并不能观察到变化,需要给数组重新赋值,比如arr = arr.concat(a)
问题解决
关于子组件的inject设置type的问题
描述: 我需要在一个子组件中使用store中的数据,于是我在子组件的props类型定义了store的类型并使用inject注入该store;
// store
export class ProductListStore {
@observable productNames: string[] = [];
}
// 子组件
interface IChild {
productListStore: IProductListStore;
}
@inject('productListStore')
@observer
class ChildComponent extends React.Component<IChild> {
render() {
const { productListStore } = this.props;
return (
<div>
{productListStore.productNames.map(name => (
<div>{name}</div>
))}
</div>
);
}
}
然而父组件没有传递该props,
// 父组件
class ParentComponent extends React.Component<any, any> {
render() {
return (
<div>
<ChildComponent />
</div>
);
}
}
于是typescript给我报错了,提示父组件应该传递该属性给子组件。
TS2741: Property 'productListStore' is missing in type '{}' but required in type 'Readonly<PropsWithChildren<IChild>>'.
解决: 上了github查看mobx-react的issues,果然找到了 https://github.com/mobxjs/mobx-react/issues/256
里面发现有两位大佬提供了解决思路和用法,然而看起来有点不好理解,
于是我暂时用了一个简易的解决方法:给子组件的props类型设置为any;
然而用了any不能自动补全了,两个小时之后,很难受,还是加上了类型,然后改为给父组件注入store,然后父组件把这个属性作为props再传递给子组件。
// 子组件
interface IChild {
productListStore: IProductListStore;
}
class ChildComponent extends React.Component<IChild> {
render() {
const { productListStore } = this.props;
return (
<div>
{productListStore.productNames.map(name => (
<div>{name}</div>
))}
</div>
);
}
}
// 父组件
interface IParent {
productListStore: IProductListStore;
}
@inject('productListStore')
@observer
class ParentComponent extends React.Component<IParent, any> {
render() {
return (
<div>
<ChildComponent productListStore={this.props.productListStore} />
</div>
);
}
}
问题解决
Antd Form 的type问题
描述: 项目中用到antd的表单,然后在创建Form的时候,不清楚如何给它写type
解决: 上stackoverflow搜到了,
https://stackoverflow.com/questions/44898248/how-to-use-antd-form-create-in-typescript
引入FormComponentProps,在Form.create的时候写自己的proptype,在渲染函数的props中写自己的proptype和FormComponentProps
import * as React from 'react';
import Form from 'antd/lib/form';
import { FormComponentProps } from 'antd/lib/form';
interface IBasicInfo {
userName: string;
//...其他属性
}
interface IBasicInfoForm {
data: IBasicInfo;
onChange: (value: string) => void;
}
const RenderComponent = (props: IBasicInfoForm & FormComponentProps) => {
const { getFieldDecorator } = props.form;
return (
<Form>
<FormItem>
{getFieldDecorator('userName')(<Input />)}
</FormItem>
</Form>
)
};
const BasicInfoForm = Form.create<IBasicInfoForm>({
name: 'basicInfoForm',
onFieldsChange(props, changedFields) {
props.onChange(changedFields);
},
mapPropsToFields(props) {
const { data } = props;
return {
userName: Form.createFormField({
value: data.userName
})
};
}
})(RenderComponent);
export default BasicInfoForm;
问题解决