reactjs - 类中未找到属性 - Flow 和 React

标签 reactjs flowtype

我正在尝试利用 Flow 并使其与我的 React 组件完美配合。但是我得到:

client/search-container.jsx:18 18: this.handleSearchSubmit = this.handleSearchSubmit.bind(this); ^^^^^^^^^^^^^^^^^^ property handleSearchSubmit. Property not found in 18: class SearchContainer extends React.Component { ^^^^^^^^^^^^^^^ SearchContainer

我设置的组件如下:

// @flow

import React, { PropTypes } from 'react';
import SearchForm from './search-form';

type Props = {
  onSearchUpdate: Function,
}


class SearchContainer extends React.Component {
  props: Props;


  constructor(props: Props) {
    super(props);
    this.handleSearchSubmit = this.handleSearchSubmit.bind(this);
  }

  handleSearchSubmit(searchTerm: {address: string, lat: number, lng: number}): void {
    this.props.onSearchUpdate(searchTerm);
  }

  render() {
    return (
      <div className="searchBox">
        <SearchForm onSearchSubmit={this.handleSearchSubmit} />
      </div>
    );
  }
}

// SearchContainer.propTypes = {
//   onSearchUpdate: PropTypes.func,
// };

export default SearchContainer;

您会看到之前我在类底部使用了 propTypes。问题:

  1. 我的类(class)设置看起来正确吗?
  2. 为什么流程提示找不到属性handleSearchSubmit,并且与我的类名SearchContainer的名称也相同

最佳答案

你好,我觉得@TLadd 的答案是一个黑客。

Flow 正在询问handleSearchSubmit 的类型,但没有找到 您只需将其添加到方法定义下方

  handleSearchSubmit: <your type>;

这是最终的代码

// @flow

import React, { PropTypes } from 'react';
import SearchForm from './search-form';

type Props = {
  onSearchUpdate: Function,
}


class SearchContainer extends React.Component {
  props: Props;
  // ->>>
  handleSearchSubmit: <your type>;
  // <---
  constructor(props: Props) {
    super(props);
    this.handleSearchSubmit = this.handleSearchSubmit.bind(this);
  }

  handleSearchSubmit(searchTerm: {address: string, lat: number, lng: number}): void {
    this.props.onSearchUpdate(searchTerm);
  }

  render() {
    return (
      <div className="searchBox">
        <SearchForm onSearchSubmit={this.handleSearchSubmit} />
      </div>
    );
  }
}

// SearchContainer.propTypes = {
//   onSearchUpdate: PropTypes.func,
// };

export default SearchContainer;

关于reactjs - 类中未找到属性 - Flow 和 React,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43022544/

相关文章:

javascript - 在单页应用中,在服务器上进行排序和过滤是标准的吗?

html - 如何在 react 中添加默认 Prop 或值(value)

node.js - 如何从 npm 发布的模块中导入流注解、类型和接口(interface)

javascript - 设置 React Native 和 Flow - 获取 Native Base 错误

javascript - onPress 错误 "is not a function"和 "is undefined"

javascript - react js : Invariant Violation: processUpdates() when rendering a table with a different number of child rows

javascript - 如何使用 useState React 进行映射

javascript - 如何根据参数字符串定义返回类型?流式javascript

flowtype - $PropertyType 获取数组条目

javascript - 使用静态字段为 React 组件创建流类型