【问题标题】:What is the proper way to set the PROPS type?设置 PROPS 类型的正确方法是什么?
【发布时间】:2020-02-18 03:21:47
【问题描述】:

我正在 REACT 中使用 REDUX,并且正在努力让 mapStateToProps 功能正常工作。

这是我的组件:

interface NavProps{
    loggedIn: boolean,
    loggingIn: boolean
}

class Navigator extends Component {

public render(){
    const { loggedIn } = this.props as NavProps;
    return (
        ...removed irrelevant code
      );
    }
}

const mapStateToProps = (state: RootReducer): NavProps => {
    const { authentication } = state;
    const { loggedIn, loggingIn } = authentication;
    return{
        loggedIn,
        loggingIn
    };
}

export default connect(mapStateToProps, null)(Navigator);

在这里你可以看到我用过:

const { loggedIn } = this.props as NavProps;

如果没有,我会收到一个错误,提示“loggedIn”不在 this.props 中。

在我看的教程中,他们都使用了这样的东西没有问题:

const { loggedIn } = this.props

我做得正确还是错过了使道具正确反映在 mapStateToProps 期间分配的内容的步骤?

更新: 我已经回顾了一些关于 REDUX 的教程。我现在看到他们使用的是 JSX,而不是 TSX,所以也许我必须使用类型断言来安抚 Typescript。

更新:

有几个人建议:

class Navigator extends Component <NavProps>

但是,这会产生此错误:

Type '{}' is missing the following properties from type 'Pick<NavProps, "loggedIn" | "loggingIn">': loggedIn, loggingIn  TS2739

<Router>
22 |       <div className="App">
> 23 |         <Navigator />

不确定是否有任何区别,但这是嵌入 NAVIGATOR 的组件:

const App = () => (
  <Provider store={store}>
    <Router>
      <div className="App">
        <Navigator />
            <Switch>
              <Route exact path="/libraries" component={Libraries} />
            </Switch>
      </div>
    </Router>
  </Provider>
);

export default App;

好的,我在这里找到了一个建议:

TS2740 Type is missing the following properties from ReadOnly error in React Native with TypeScript app

从下面的 cmets 中看起来很奇怪,答案是这样做:

class Navigator extends Component<any, any> 

即使只是在其中粘贴“任何”似乎并不正确。

这是一个更清晰一点的:

TS2739 - Type missing in following properties

它建议将道具设为可选:

interface NavProps{
    loggedIn?: boolean,
    loggingIn?: boolean
}

class Navigator extends Component<NavProps>

【问题讨论】:

  • class Navigator extends Component&lt;AuthenticationState&gt; 怎么样?
  • @Andus 我相信应该有 2 个作业,第一个用于状态,第二个用于道具。当我进行您建议的更改时,出现此错误:“类型'{}'缺少类型'Pick'中的以下属性:loggedIn,loggingIn”
  • 我检查了component 的类型,我猜第一个是道具,第二个是状态,因为它写道:interface Component&lt;P = {}, S = {}, SS = any&gt;,我猜P 是道具
  • 谢谢@Andus。我看到了同样的事情,当它只是反过来起作用时,我感到很惊讶。我做了一些更改,我只为道具使用界面。现在我什至不需要使用这样的作业。但是,我仍然有同样的问题,我必须使用 'this.props as NavProps'

标签: reactjs typescript react-redux


【解决方案1】:

我们可以在the TypeScript docs here看到:

class Navigator extends Component<NavProps> {

这是它在我的机器上的样子:

【讨论】:

  • 我收到以下错误:类型 '{}' 缺少来自类型 'Pick': 登录,登录 TS2739 21 | 22 |
    > 23 | | ^ 24 | 25 | 26 | 开关>
  • 是的,我完全不明白。如果我使用 ,我会收到上面的错误。如果我将其更改为 它不会给我一个错误,但我仍然必须使用类型断言。
  • @LeeLoftiss 我添加了一张可能对您有所帮助的图片。 React 很棘手。
  • 是的,告诉我。
【解决方案2】:

根据我上面的cmets,我已经测试过了,完全没有问题

interface NavProps {
  loggedIn: boolean
  loggingIn: boolean
}

class Navigator extends Component <NavProps> {
  public render() {
    const { loggedIn } = this.props
    console.log(loggedIn)
    return null
  }
}

const mapStateToProps = (state: RootReducer): NavProps => {
  const { authentication } = state
  const { loggedIn, loggingIn } = authentication
  return {
    loggedIn,
    loggingIn,
  }
}

export default connect(mapStateToProps, null)(Navigator)

除了你的问题中我没有 RootReducer 类型


更新:也尝试像这样写connect

export default connect<NavProps>(mapStateToProps, null)(Navigator)

【讨论】:

  • 我在尝试时收到以下错误:Type '{}' is missing the following properties from type 'Pick': 登录,登录 TS2739 21 | 22 |
    > 23 | | ^ 24 | 25 | 26 | 开关>
  • 在您上面的评论中,您说使用AuthenticationState 而不是NavProps
  • 因为op改了接口@Jacob的名字
  • 哦,抱歉没注意到。
  • @Jacob 谢谢。是的,我将其更改为同一文件中的接口。不知道为什么,但这样做消除了需要,似乎在类名中添加了类型。另外,这似乎更好,因为稍后我可能会在道具中添加更多不属于 AuthenticationState 的项目。
猜你喜欢
相关资源
最近更新 更多
热门标签