Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何为Immutable.js对象编写TypeScript接口?_Reactjs_Typescript_Immutable.js_Typechecking - Fatal编程技术网

Reactjs 如何为Immutable.js对象编写TypeScript接口?

Reactjs 如何为Immutable.js对象编写TypeScript接口?,reactjs,typescript,immutable.js,typechecking,Reactjs,Typescript,Immutable.js,Typechecking,假设我们有以下界面User: interface User { id: number; name: string; bag: Item[]; } 我现在可以定义一个React组件: interface UserComponentProps { user: User; } interface UserComponentState {} class UserComponent extends React.Component<UserComponentProps, UserC

假设我们有以下界面
User

interface User {
  id: number;
  name: string;
  bag: Item[];
}
我现在可以定义一个React组件:

interface UserComponentProps {
  user: User;
}
interface UserComponentState {}

class UserComponent extends React.Component<UserComponentProps, UserComponentState> {
接口UserComponentProps{
用户:用户;
}
接口UserComponentState{}
类UserComponent扩展了React.Component{
这很好,因为现在我可以在组件中进行类型检查,但有一个问题:用户不是不可变的,所以当用户没有更改时,很难阻止UserComponent呈现

但如果我将用户存储为不可变对象,我的组件将如下所示:

interface UserComponentProps {
  user: Immutable.Map<string, any>;
}
interface UserComponentState {}

class UserComponent extends React.Component<UserComponentProps, UserComponentState> {
接口UserComponentProps{
用户:Immutable.Map;
}
接口UserComponentState{}
类UserComponent扩展了React.Component{

如何为此类对象定义接口?

如果
道具发生更改,React的组件将重新渲染,因此答案很大程度上取决于如何创建用于渲染的
道具

一个简单的例子可以给你一个想法:

constbag=Immutable.List([{name:“bagItem1”}]);
console.log(bag.toJS()==bag.toJS())

完全不使用Immutable可能是一个不错的选择,事实上,我确实认为这会导致应用程序效率低下,但你没有回答我的问题,即如何使用Immutable进行键入(这有助于记录并允许进行键入检查)。当用户未更改时,很难阻止UserComponent呈现。这是我回答的部分。我相信您误解了接口在这里的作用。接口无法影响组件重新呈现的时间。但如果您仍然只想键入,请检查Immutable的
记录
,示例和