[英]Switch from random order to array order
如何更改數組內的順序? 我有這個
setColor(colors[Math.floor(Math.random() * colors.length)]);
我想根據數組 position 而不是隨機排序。 謝謝
查看當前正在使用哪種顏色並切換到另一種顏色。
const [color, setColor] = useState(false); //or maybe useState("#ff6700")
function onClick() {
const colors = ["#ff6700", "#ffffff"];
if ( colors.indexOf(color) === 0 ) {
setColor(colors[1]);
} else {
setColor(colors[0]);
}
}
希望能幫助到你:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.