【问题标题】:How to resolve TS2345 compile error in React如何解决 React 中的 TS2345 编译错误
【发布时间】:2019-08-24 20:29:12
【问题描述】:

我尝试在React 中编写 setState 方法。
但是出现编译错误。
我想知道如何解决这个问题。

前端:react/typesript

articleApi.tsx

import axios from 'axios';
import {Article} from '../articleData';

export const getSingleArticleFactory = (id: string) => {
  const getSingleArticle = async (id: string) => {
    try {
      const response = await axios.get('/api/article/' + id);

      if (response.status !== 200) {
        throw new Error('Server Error');
      }
      const article: Article = response.data;

      return article;
    } catch (err) {
      throw err;
    }
  };
  return getSingleArticle;
};

Detail.tsx

//import

interface ArticleState {
  article: Article;
  redirect: boolean;
  user: firebase.User | null;
}

class Detail extends React.Component<
  RouteComponentProps<{id: string}>,
  ArticleState
> {
  constructor(props: RouteComponentProps<{id: string}>) {
    super(props);
    this.state = {
      article: {
        id: 0,
        title: '',
        content: '',
        imageNames: [],
      },
      redirect: false,
      user: null,
    };
    this.getArticle = this.getArticle.bind(this);
    this.deleteArticle = this.deleteArticle.bind(this);
  }

  getArticle() {
    this.setState({
      //error occures here
      article: api.getSingleArticleFactory(this.props.match.params.id);,
    });
  }

articleData.tsx

export interface Article {
  id: number;
  title: string;
  content: string;
  imageNames: ImageName[];
}

interface ImageName {
  name: string;
}

我希望没有编译错误。
但实际并非如此。
我想知道如何解决这个问题。

const getSingleArticleFactory: (id: string) => (id: string) => Promise<Article>
Argument of type '{ article: (id: string) => Promise<Article>; }' is not assignable to parameter of type 'ArticleState | ((prevState: Readonly<ArticleState>, props: Readonly<RouteComponentProps<{ id: string; }, StaticContext, any>>) => ArticleState | Pick<ArticleState, "article"> | null) | Pick<...> | null'.
  Type '{ article: (id: string) => Promise<Article>; }' is not assignable to type 'Pick<ArticleState, "article">'.
    Types of property 'article' are incompatible.
      Type '(id: string) => Promise<Article>' is missing the following properties from type 'Article': id, title, content, imageNamests(2345)

【问题讨论】:

    标签: javascript reactjs typescript


    【解决方案1】:

    我通过以下方式修复它:

      async getArticle() {
        const getSingleArticle = api.getSingleArticleFactory();
        const articleData = await getSingleArticle(this.props.match.params.id);
        this.setState({
          article: articleData,
        });
      }
    
    export const getSingleArticleFactory = () => {
      const getSingleArticle = async (id: string) => {
        try {
          const response = await axios.get('/api/article/' + id);
    
          if (response.status !== 200) {
            throw new Error('Server Error');
          }
          const article: Article = response.data;
    
          return article;
        } catch (err) {
          throw err;
        }
      };
      return getSingleArticle;
    };
    

    【讨论】:

      猜你喜欢
      • 2019-09-04
      • 2013-03-17
      • 1970-01-01
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      相关资源
      最近更新 更多