We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Code Review Feedback
Code Review Feedback
Sort by
recency
|
72 Discussions
|
Please Login in order to post a comment
This is my FeedBack Component
const FeedbackSystem = () => { // const [Upvote, setUpvote] = React.useState(0) // const [Downvote, setDownvote] = React.useState(0) const [cards, setCards] = React.useState( [ { id: 0, name: "Readability", Upvote: 0, Downvote: 0 }, { id: 1, name: "Performance", Upvote: 0, Downvote: 0 }, { id: 2, name: "Security", Upvote: 0, Downvote: 0 }, { id: 3, name: "Documentation", Upvote: 0, Downvote: 0 }, { id: 4, name: "Testing", Upvote: 0, Downvote: 0 }, ])
function upVote(id) { //setUpvote(prev => prev + 1)
}
function downVote(id) { //setDownvote(prev => prev + 1) setCards(prev => prev.map((card, index) => ( index == id ? { ...card, Downvote: card.Downvote + 1 } : card ))) }
return ( { cards.map((card, index) => (
{card.name}
upVote(index)} downVote={() => downVote(index)} id={index} />); };
export default FeedbackSystem;
This is my Button Component
export default function Button({upVote,downVote,id}) {
return ( <> upvote-btn-{id}} onClick={downVote}> 👎 Downvote ) } Where I made misatke , my test cases are failing
WWhere
import React, {useState} from "react";
const aspects = ["Readability", "Performance", "Security", "Documentation", "Testing"];
const FeedbackSystem = () => { const [feedback, setFeedback] = useState( aspects.map(() => ({ upvotes: 0, downvotes: 0 })) );
const handleUpvote = (index) => { const newFeedback = [...feedback]; newFeedback[index].upvotes ++; setFeedback(newFeedback); };
const handleDownvote = (index) => { const newFeedback = [...feedback]; newFeedback[index].downvotes ++; setFeedback(newFeedback); }; return ( {aspects.map((aspect, index) => (
{aspect}
upvote-btn-{index}} onClick={() => handleDownvote(index)}> 👎 Downvote upvote-btn-${index}}> Upvotes: {feedback[index].upvotes} downvote-count-${index}}> Downvotes: {feedback[index].downvotes} ))} ); };export default FeedbackSystem;
Here is my simple solution.
`import React, { useState } from "react";
const FeedbackSystem = () => { const list = [ { label: "Readability", upVote: 0, downVote: 0 }, { label: "Performance", upVote: 0, downVote: 0 }, { label: "Security", upVote: 0, downVote: 0 }, { label: "Documentation", upVote: 0, downVote: 0 }, { label: "Testing", upVote: 0, downVote: 0 } ]; const [aspectList, setAspectList] = useState(list);
const handleDownVote = (index) => { aspectList[index].downVote += 1; setAspectList([...aspectList]) }
const handleUpVote = (index) => { aspectList[index].upVote += 1; setAspectList([...aspectList]) }
return ( {aspectList.map((aspect, index) => (
{aspect.label}
upvote-btn-{index}} onClick={() => handleDownVote(index)}> 👎 Downvote upvote-count-${index}}> Upvotes: {aspect.upVote} downvote-count-${index}}> Downvotes: {aspect.downVote} ))} ); };export default FeedbackSystem;
`
What's wrong with this code? I've used a reducer instead of useState hook.
data-testid="downvote-count-0"> dont forget to change id