Why am I receiving this error "Uncaught TypeError: state.map is not a function" it only happens when I set my setState in my handleClick function

Viewed 24

It's saying state.map is not a function but it works just fine before i call the handleclick function.I am stuck here and would like to know why its giving me the state.map is not a function error. Added bonus too would be me asking how does one setState when you have objects inside objects. For example if id like to return with setStatea {...item, value:false} value is inside and object thats inside and object. How would I specifically target the value I need inside and array of objects. Thank you.

import React from "react";
import Quiz from "./components/Quiz";
import Menu from "./components/Menu";
import { v4 as uuidv4 } from 'uuid'
function App() {

  
  const [state,setState] = React.useState([])
  const [start,setStart] = React.useState(false)

    React.useEffect(()=> {
      if(start === true){
        console.log(state)
      fetch("https://opentdb.com/api.php?amount=5&category=27&difficulty=easy&type=multiple")
      .then(res => res.json())
      
      .then(data => setState(data.results.map(item =>
      ({selectedQuestion: "",
          buttons: item.incorrect_answers.concat(item.correct_answer).map(item => 
            ({use: item ,value: false, id: uuidv4()})),
          questions: item.question   
          
      }))))
    }} , [start])
  function startGame () {  
    setStart(prevState => !prevState)
}

const myButtons = state.map(function(item) { 
  return (<Quiz 
  key={uuidv4()}
  buttons={item.buttons}
  value ={item.buttons.map(item => item.value)}
  question = {item.questions}
  handleClick ={(event) =>handleClick(event.target.value)}

/> ) })
console.log(state.map(item => item))
 
  function handleClick (event) {
    if (event === "false"){
      
//this is what is giving me issues//  
    setState(item => (
    {
      ...item, selectedQuestion: "howdy"
    }))
//////////////////////////////////////////////////////////////
     }
    else {
      console.log("hi")
      }

      }


  return (<div>
    {start === false ? <Menu  
    startGame ={startGame}
    /> : <div className="parent--quiz">
    <div className="quiz">
      {myButtons}
   
      
    
   </div>
   </div>
        }
   </div>
  )
}

export default App;



Quiz component is here 

function Quiz (props) {
   
    const styles ={
        backgroundColor: props.value === true ? "red" : "#D6DBF5"
    }
    
    return (
        
        <div>
             <div className="cards" style={{ borderTop: "2px solid #fff ", marginLeft: 20, marginRight: 20 }}>
        <h1 className="quiz--h1">{props.question}</h1>
        <div className="quiz--buttons-div" >
        {props.buttons.map((item) => (
            <button className="quiz--buttons" style={styles} value={item.value} onClick={props.handleClick}>{item.use}</button>
        ))}
        </div>
        </div>
        </div>
        
        
    )
}
export default Quiz
code sections are fine

0 Answers
Related