Skip to content

Commit

Permalink
Star Rating
Browse files Browse the repository at this point in the history
Fix - pass arguments by destructuring
  • Loading branch information
jessilyneh committed Mar 1, 2022
1 parent 4d03768 commit 8071bf5
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
import React, { useState } from "react"; //useState is a function, this is why i use { }
import React, {useState} from "react"; //useState is a function, this is why i use { }
import ReactDOM from "react-dom";
import "./index.css";
import {FaStar} from "react-icons/fa"

const createArray = (length) => [
...Array(length)]
...Array(length)];

function Star({selected = false, onSelect}) {
return (
<FaStar
<FaStar
color={selected ? "yellow" : "gray"}
onClick={onSelect}
/>
)
}

function StarRating(totalStars = 5) { //default value, in case totalStars hasnt a value defined in return
function StarRating({totalStars = 5}) { //default value, in case totalStars hasnt a value defined in return
const [selectedStars, setSelectedStars] = useState(0)

return (
<>
{createArray(totalStars).map((_n,i) => (
<Star
key={i}
selected={selectedStars > i}
onSelect={()=> setSelectedStars(i + 1)}
/>
{
createArray(totalStars).map((_n,i) => (
<Star
key={i}
selected={selectedStars > i}
onSelect={()=> setSelectedStars(i + 1)}
/>
)
)
)
}
<p>{selectedStars} of {totalStars}</p>
}
<p>{selectedStars} of {totalStars}</p>
</>
)
}
Expand All @@ -39,7 +42,7 @@ function App() {

ReactDOM.render(
<React.StrictMode>
<App name="jess" />
<App />
</React.StrictMode>,
document.getElementById("root")
)

0 comments on commit 8071bf5

Please sign in to comment.