【问题标题】:Property 'value' does not exist on type 'Readonly<{}>'类型“只读<{}>”上不存在属性“值”
【发布时间】:2018-05-13 16:43:53
【问题描述】:

我需要创建一个表单,该表单将根据 API 的返回值显示某些内容。我正在使用以下代码:

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {value: ''};

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange(event) {
    this.setState({value: event.target.value});
  }

  handleSubmit(event) {
    alert('A name was submitted: ' + this.state.value); //error here
    event.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Name:
          <input type="text" value={this.state.value} onChange={this.handleChange} /> // error here
        </label>
        <input type="submit" value="Submit" />
      </form>
    );
  }
}

我收到以下错误:

error TS2339: Property 'value' does not exist on type 'Readonly<{}>'.

我在评论代码的两行中遇到了这个错误。这段代码甚至不是我的,我是从 react 官方网站 (https://reactjs.org/docs/forms.html) 获得的,但在这里不起作用。

我正在使用 create-react-app 工具。

【问题讨论】:

标签: reactjs typescript


【解决方案1】:

Componentis defined 是这样的:

interface Component<P = {}, S = {}> extends ComponentLifecycle<P, S> { }

表示状态(和道具)的默认类型是:{}
如果您希望您的组件在状态中具有value,那么您需要像这样定义它:

class App extends React.Component<{}, { value: string }> {
    ...
}

或者:

type MyProps = { ... };
type MyState = { value: string };
class App extends React.Component<MyProps, MyState> {
    ...
}

【讨论】:

  • 天哪,老兄,它现在起作用了,再回答我一件事,这个语法与 TypeScript 有关,对吧?因为在 react 官方网站上没有类似的东西
  • 是的,这与打字稿严格相关。
  • 正确的定义是:class Square extends React.Component { ... }
  • @NitzanTomer - 你拯救了我的一天
【解决方案2】:
interface MyProps {
  ...
}

interface MyState {
  value: string
}

class App extends React.Component<MyProps, MyState> {
  ...
}

// Or with hooks, something like

const App = ({}: MyProps) => {
  const [value, setValue] = useState<string>('');
  ...
};

type's 也很好,就像@nitzan-tomer 的回答一样,只要你保持一致。

【讨论】:

  • 请总结一下在您的帖子中一致意味着什么,这样就可以在无需阅读中篇文章的情况下获得其全部价值(这是一个非常有用的链接,谢谢)。
【解决方案3】:

如果你不想传递接口状态或道具模型,你可以试试这个

class App extends React.Component <any, any>

【讨论】:

    【解决方案4】:

    我建议使用

    仅用于字符串状态值

    export default class Home extends React.Component<{}, { [key: string]: string }> { }
    

    用于字符串键和任何类型的状态值

    export default class Home extends React.Component<{}, { [key: string]: any}> { }
    

    对于任何键/任何值

    export default class Home extends React.Component<{}, { [key: any]: any}> {}
    

    【讨论】:

      【解决方案5】:

      问题是你还没有声明你的接口状态 用你合适的“值”变量类型替换任何变量

      Here is a good reference

      interface AppProps {
         //code related to your props goes here
      }
      
      interface AppState {
         value: any
      }
      
      class App extends React.Component<AppProps, AppState> {
        // ...
      }
      

      【讨论】:

        【解决方案6】:

        根据官方 ReactJs 文档,您需要以默认格式传递参数:

        P = {} // default for your props
        S = {} // default for yout state
        
        interface Component<P = {}, S = {}> extends ComponentLifecycle<P, S> { }
        

        或者像下面这样定义你自己的类型:(只是一个 exp)

        interface IProps {
            clients: Readonly<IClientModel[]>;
        
            onSubmit: (data: IClientModel) => void;
        }
        
        interface IState {
           clients: Readonly<IClientModel[]>;
           loading: boolean;
        }
        
        class ClientsPage extends React.Component<IProps, IState> {
          // ...
        }
        

        typescript and react whats react component P S mean

        how to statically type react components with typescript

        【讨论】:

          【解决方案7】:

          event.targetEventTarget 类型,它并不总是有值。如果它是一个 DOM 元素,则需要将其转换为正确的类型:

          handleChange(event) {
              this.setState({value: (event.target as HTMLInputElement).value});
          }
          

          这将推断出状态变量的“正确”类型,尽管显式可能更好

          【讨论】:

          • 我认为他在尝试初始化构造函数中的字符串时遇到错误,而不是事件处理程序
          猜你喜欢
          • 1970-01-01
          • 2020-05-03
          • 2018-12-04
          • 2021-02-03
          • 2019-02-14
          • 2020-01-29
          • 2017-06-23
          • 2019-02-17
          • 2017-10-06
          相关资源
          最近更新 更多