我想使用联合类型的键作为打字稿中对象的键。
type EnumType = 'a1' | 'a2'
const object:{[key in EnumType]: string}= {
a1: 'test'
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我什至必须添加 a2 作为对象中的键。有没有办法让它成为可选的?
我试图从 useState 钩子访问状态,但即使在我修改它之后它也给了我初始状态。
const quotesURL = "https://gist.githubusercontent.com/camperbot/5a022b72e96c4c9585c32bf6a75f62d9/raw/e3c6895ce42069f0ee7e991229064f167fe8ccdc/quotes.json";
function QuoteGenerator() {
const [quotes, setQuotes] = useState([]);
const [currentQuote, setCurrentQuote] = useState({ quote: "", author: "" });
useEffect(() => {
axios(quotesURL)
.then(result => {
console.log(result);
setQuotes(result.data);
})
.then(() => {
console.log(quotes);
});
}, []);
Run Code Online (Sandbox Code Playgroud)
console.log(quotes) 返回空数组而不是对象数组
目前,我有两个使用 gatsby.js 开发的站点部署到 example.com 和 blog.example.com。我想创建一个 blog.example.com 的子目录到 example.com/blog。
有没有办法在不合并两个存储库的情况下做到这一点?
我是Dart / Flutter框架的新手,我仍在探索它们的可能性。
我知道在Android中可以拍照并以编程方式从中提取主要颜色值。(以Android为例)
我想知道,在纯Dart中如何实现?我希望它与iOS和Android操作系统兼容。
URLSearchParamstypescript中的append 方法的类型append(name: string, value: string): void;。
我尝试附加数组和数字,它在浏览器中对我有用,但在打字稿代码中给出错误。
在 MDN 中,我找到了一个示例,其中将数字用作值https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/append
我想知道我们是否可以使用字符串以外的内容作为打字稿中的值
我想一个接一个地排列项目。但它只显示数组的最后一个元素。
预期输出在 2 秒后显示“a”“b”,依此类推。
class Main extends React.Component {
constructor(props) {
super(props)
this.state = {
role: ["a","b","c"],
display: "",
}
}
componentDidMount() {
for (let i of this.state.role) {
setTimeout(() => {
this.setState({ display: i})
}, 2000)
}
}
render() {
return (
<div>
<h3>{this.state.display}</h3>
</div>
)
}
}
ReactDOM.render(<Main />, document.getElementById('root'))Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id='root'></div>Run Code Online (Sandbox Code Playgroud)
javascript ×2
reactjs ×2
types ×2
typescript ×2
android ×1
dart ×1
flutter ×1
gatsby ×1
ios ×1
react-hooks ×1