简体   繁体   English

flowtype不抱怨缺少React prop

[英]flowtype not complaining about missing React prop

I have the following Counter example: 我有以下计数器示例:

/**
 * @flow
 */

import React, { Component } from 'react'
import { connect } from 'react-redux'
import { increment, decrement } from '../actions/counter'
import type { State } from '../store'

type Props = {
  counter: number,
  increment: Function,
  decrement: Function,
  hello: Function // There is no `hello` prop coming in to the component, but there doesn't seem to be an error
}

class Counter extends Component<*, Props, *> {
  props: Props

  render() {
    const { counter, increment, decrement } = this.props

    return (
      <div>
        <h1>{counter}</h1>
        <button onClick={decrement}>-</button>
        <button onClick={increment}>+</button>
      </div>
    )
  }
}

function mapStateToProps(state: State) {
  return {
    counter: state.counter
  }
}

function mapDispatchToProps(dispatch: Function) {
  return {
    increment: () => dispatch(increment()),
    decrement: () => dispatch(decrement())
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(Counter)

I have a required hello prop field in my Props type, however I don't seem to be getting any errors even though the component is not receiving a hello prop. 我的Props类型中有一个必填的hello prop字段,但是,即使该组件未收到hello prop,我似乎也没有收到任何错误。 Why is this happening? 为什么会这样呢?

This is showing the error of the missing properties. 这显示缺少属性的错误。 I changed <*, Props, *> to <void, Props, void> . 我将<*, Props, *>更改为<void, Props, void> And export the Counter class without Redux. 并导出没有Redux的Counter类。

 // @flow
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { increment, decrement } from '../actions/counter'
import type { State } from '../store'

type Props = {
  counter: number,
  increment: Function,
  decrement: Function,
  hello: Function // There is no `hello` prop coming in to the component, but there doesn't seem to be an error
}

// added export for Counter and void instead of *
export class Counter extends Component<void, Props, void> {
  props: Props;

  render() {
    const { counter, increment, decrement } = this.props

    return (
      <div>
        <h1>{counter}</h1>
        <button onClick={decrement}>-</button>
        <button onClick={increment}>+</button>
      </div>
    )
  }
}

function mapStateToProps(state: State) {
  return {
    counter: state.counter
  }
}

function mapDispatchToProps(dispatch: Function) {
  return {
    increment: () => dispatch(increment()),
    decrement: () => dispatch(decrement())
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(Counter)

Class that use the counter 使用计数器的类

// @flow
import React, { Component } from 'react';
import {Counter} from './counter';

class MainCounter extends Component
{

 render ()
 {
  return (
   <div>
    <Counter />
   </div>
  );
 }
}

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM