小编JaL*_*aLe的帖子

如何设置 props 的默认值

下面的代码获取 URL 数据并检索所有持续时间以秒为单位的电影列表但我只想列出持续时间超过 5000 秒的电影 *该组件应该有一个 prop minDuration 并且仅列出持续时间超过 5000 秒的电影应列出该值;该属性的默认值应该是 5000

您能澄清一下如何解决这个问题吗

    import React, { useState, useEffect } from "react";

export default function App() {
  const [films, setFilms] = useState([]);
  
  useEffect(() => {
    const listOfFilms = async () => {
      const response = await fetch(
        "https://api.flixpremiere.com/v1/films/filter/now_showing?limit=10"
      );
      console.log("here", response.data);
      const jsonresponse = await response.json();
      console.log("here11", jsonresponse.films);
      setFilms(jsonresponse.films);
    };
    listOfFilms();
  }, []);
  return (
    <div className="App">
      <h1>Film Title</h1>
      {console.log("films", films.slug, films.films)}

      {films.map((film, index) => (
          <ul>
            <li key={film.title}>
           {film.title} {`(${film.duration_seconds})`} …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs react-props

6
推荐指数
2
解决办法
1万
查看次数

如何在打字稿中创建枚举值作为类型/接口?

我有枚举:

enum DemoEnum {
    a = 'EnumValueA',
    b = 'EnumValueB'
}
Run Code Online (Sandbox Code Playgroud)

我想type Type = 'EnumValueA' | 'EnumValueB'从我的枚举值创建。

我怎样才能做到这一点?

我当前的状态是“键”类型:

type Type = keyof typeof DemoEnum // 'a' | 'b'

例如,我想在我的反应道具中使用它。

type Props {
   value: 'EnumValueA' | 'EnumValueB',
}
Run Code Online (Sandbox Code Playgroud)

使用时<MyComponent value='EnumValueA'>

type Props {
   value: DemoEnum,
}
Run Code Online (Sandbox Code Playgroud)

我收到错误Type .. is not assignable to DemoEnum

enums typescript

6
推荐指数
2
解决办法
7631
查看次数

标签 统计

enums ×1

javascript ×1

react-props ×1

reactjs ×1

typescript ×1