Reactjs 不应该';Eslint是否在React中将道具字段强制为只读?
假设我有以下代码片段:Reactjs 不应该';Eslint是否在React中将道具字段强制为只读?,reactjs,typescript,eslint,Reactjs,Typescript,Eslint,假设我有以下代码片段: interface MyProps { label: string; } function MyComponent(props: MyProps) { return ( <p> {props.label} </p> ); } 接口MyProps{ 标签:字符串; } 功能MyComponent(道具:MyProps){ 返回( {props.label} ); } 如果我没有将Props中的字段标记
interface MyProps {
label: string;
}
function MyComponent(props: MyProps) {
return (
<p>
{props.label}
</p>
);
}
接口MyProps{
标签:字符串;
}
功能MyComponent(道具:MyProps){
返回(
{props.label}
);
}
如果我没有将Props
中的字段标记为readonly
,Eslint和TypeScript编译器都不会抛出警告或错误。但是,永远不应该修改组件道具,否则React UI引擎将无法按预期工作
这是我的Eslint@6.8.0, React@16.13.1及TypeScript@3.9.3.它应该已经包含在规则eslint中(无参数重新分配) “我的编辑器”给出如下eslint错误消息:
import React from 'react';
interface MyProps {
label: string;
}
export function MyComponent(props: MyProps): React.ReactElement {
props.label = 'hi'; // Assignment to property of function parameter 'props'.eslint(no-param-reassign)
return <p>{props.label}</p>;
}
从“React”导入React;
接口MyProps{
标签:字符串;
}
导出函数MyComponent(props:MyProps):React.ReactElement{
props.label='hi';//赋值给函数参数'props.eslint的属性(无参数重新赋值)
返回{props.label};
}
很好,不过如果界面首先缺少readonly
属性的话,我希望Eslint会抱怨。