【问题标题】:interface states and props in typescript react打字稿中的界面状态和道具反应
【发布时间】:2016-08-13 04:52:54
【问题描述】:

我正在开发一个使用 TypeScript 和 React 的项目,我对这两个项目都很陌生。我的问题是关于 TypeScript 中的界面以及它与道具和状态的关系。实际发生了什么?除非我声明接口道具和状态,否则我的应用程序根本不会运行,但是我通过 React 构造函数使用状态,并且我已经看到了所有这些信息都将进入“接口 MyProps”或“接口 MyStates”的示例.以这段代码为例:

"use strict";

import * as React from 'react'
import NavBar from './components/navbar.tsx'
import Jumbotron from './components/jumbotron.tsx';
import ContentPanel from './components/contentPanel.tsx';
import Footer from './components/footer.tsx';

interface MyProps {}
interface MyState {}
class Root extends React.Component <MyProps, MyState>  {
  constructor(props) {
    super(props);
    this.state = {
      ///some stuff in here
  
    };
  }
  render() {
    return (
      <div>
        <NavBar/>
        <Jumbotron content={this.state.hero}/>
        <ContentPanel content={this.state.whatIs}/>
        <ContentPanel content={this.state.aboutOne}/>
        <ContentPanel content={this.state.aboutTwo}/>
        <ContentPanel content={this.state.testimonial}/>
        <Footer content={this.state.footer}/>
      </div>
    )
  }
}
export default Root;

(我删除了 this.state 中的内容只是为了在此处发布)。为什么需要接口?这样做的正确方法是什么,因为我认为我是以 JSX 方式而不是 TSX 方式来考虑这个的。

【问题讨论】:

  • 我也在为这种模式苦苦挣扎,想知道如何将接口道具(此处为 MyProps)从外部传递给 Root 组件 - 当组件在 jsx 中创建时。遗憾的是,接受的答案没有显示那部分。
  • 更新:找到它,您使用 propName = {...} 语法。以防万一有人有同样的挣扎。
  • 错误(您可以粘贴以详细说明问题)可能与打字稿要求属性接口(@98​​7654321@)有关,例如看到这个答案stackoverflow.com/a/46987987/2361131

标签: reactjs typescript jsx tsx


【解决方案1】:

不清楚你到底在问什么,但是:

props:是从组件的父组件传递的键/值对,组件不应更改它自己的 props,只对父组件的 props 更改做出反应。

state:有点像 props,但它们在组件本身中使用 setState 方法进行了更改。

render 方法在 props 或 state 发生变化时被调用。

至于打字稿部分,React.Component 采用两种类型作为泛型,一种用于道具,一种用于状态,您的示例应该看起来更像:

interface MyProps {}

interface MyState {
    hero: string;
    whatIs: string;
    aboutOne: string;
    aboutTwo: string;
    testimonial: string;
    footer: string;
}

class Root extends React.Component <MyProps, MyState>  {
    constructor(props) {
        super(props);

        this.state = {
            // populate state fields according to props fields
        };
    }

    render() {
        return (
            <div>
                <NavBar/>
                <Jumbotron content={ this.state.hero } />
                <ContentPanel content={ this.state.whatIs } />
                <ContentPanel content={ this.state.aboutOne } />
                <ContentPanel content={ this.state.aboutTwo } />
                <ContentPanel content={ this.state.testimonial } />
                <Footer content={ this.state.footer } />
            </div>
        )
    }
}

如您所见,MyState 接口定义了稍后在组件this.state 成员中使用的字段(我将它们全部设为字符串,但它们可以是任何你想要的)。

我不确定这些字段是否真的需要在 state 中而不是在 props 中,但这就是你要做的。

【讨论】:

  • 谢谢,这一切都为我解答。
  • MyProps 有什么用?
  • @BahadurSinghDeol 反应组件属性的类型。在这个例子中它是空的,但是你可以放任何你需要的东西。
  • 问题是我可以将另一个提交到不在接口中的类状态并在没有任何类型检查的情况下使用它和项目 tun?
  • 帮了我很多!
猜你喜欢
  • 2020-08-21
  • 2021-05-10
  • 2022-08-24
  • 1970-01-01
  • 2021-12-04
  • 1970-01-01
  • 1970-01-01
  • 2018-10-26
  • 1970-01-01
相关资源
最近更新 更多