skip to Main Content

Cannot read properties of undefined (reading ‘color’)
TypeError: Cannot read properties of undefined (reading ‘color’)

import React, {useState} from 'react'

export default function About() {

    const {myStyle, setMyStyle} = useState({
        color: 'black',
        backgroundColor: 'white'
    })
    const [btntext, setBtnText] = useState("Enable Dark Mode")

    const toggleStyle = ()=>{
        if(myStyle.color === 'black'){
            setMyStyle({
                color: 'white',
        backgroundColor: 'black',
            })
            setBtnText("Enable Light Mode")
        }
        else{
            setMyStyle({
                color: 'black',
        backgroundColor: 'white',
            })
            setBtnText("Enable Dark Mode");
        }
    }

  return (
        <div className ="container" style={myStyle}>
            <h1 className='my-3'>About us</h1>
            <div className="accordion accordion-flush" id="accordionFlushExample" style={myStyle}>
                <div className="accordion-item">
                 <h2 className="accordion-header">
            <button className="accordion-button collapsed" type="button" style={myStyle} data-bs-toggle="collapse" data-bs-target="#flush-collapseOne" aria-expanded="false" aria-controls="flush-collapseOne">
                Accordion Item #1
            </button>
            </h2>
            <div id="flush-collapseOne" className="accordion-collapse collapse" data-bs-parent="#accordionFlushExample">
            <div className="accordion-body" style={myStyle}>Placeholder content for this accordion, which is intended to demonstrate the <code>.accordion-flush</code> className. This is the first item's accordion body.</div>
            </div>
                 </div>
            <div className="accordion-item"> 
            <h2 className="accordion-header">
            <button className="accordion-button collapsed" type="button" style={myStyle} data-bs-toggle="collapse" data-bs-target="#flush-collapseTwo" aria-expanded="false" aria-controls="flush-collapseTwo">
                Accordion Item #2
            </button>
            </h2>
            <div id="flush-collapseTwo" className="accordion-collapse collapse" data-bs-parent="#accordionFlushExample">
            <div className="accordion-body" style={myStyle}>Placeholder content for this accordion, which is intended to demonstrate the <code>.accordion-flush</code> className. This is the second item's accordion body. Let's imagine this being filled with some actual content.</div>
            </div>
        </div>
        <div className="accordion-item">
            <h2 className="accordion-header">
            <button className="accordion-button collapsed" type="button" style={myStyle} data-bs-toggle="collapse" data-bs-target="#flush-collapseThree" aria-expanded="false" aria-controls="flush-collapseThree">
                Accordion Item #3
            </button>
            </h2>
            <div id="flush-collapseThree" className="accordion-collapse collapse" data-bs-parent="#accordionFlushExample">
            <div className="accordion-body" style={myStyle}>Placeholder content for this accordion, which is intended to demonstrate the <code>.accordion-flush</code> className. This is the third item's accordion body. Nothing more exciting happening here in terms of content, but just filling up the space to make it look, at least at first glance, a bit more representative of how this would look in a real-world application.</div>
            </div>
        </div>
            </div>
            <div className="container my-3">
            <button onClick={toggleStyle} type ="button" className="btn btn-primary">{btntext}</button>
            </div>
         </div>
  )
}

2

Answers


  1. You need [] and not {} around the returned value from useState.

    So [myStyle, setMyStyle] instead of {myStyle, setMyStyle}

    useState always returns an array with value at index 0 and the setter function at index 1. (regardless of the type of the state)

    Read https://react.dev/reference/react/useState#returns

    Login or Signup to reply.
  2. just use [] instead of {} for [myStyle, setMyStyle]

    import React, {useState} from 'react';
    
    
    export function App(props) {
    
    
    
        const [myStyle, setMyStyle] = useState({
            color: 'black',
            backgroundColor: 'white'
        })
        const [btntext, setBtnText] = useState("Enable Dark Mode")
    
        const toggleStyle = ()=>{
            if(myStyle.color === 'black'){
                setMyStyle({
                    color: 'white',
            backgroundColor: 'black',
                })
                setBtnText("Enable Light Mode")
            }
            else{
                setMyStyle({
                    color: 'black',
            backgroundColor: 'white',
                })
                setBtnText("Enable Dark Mode");
            }
        }
    
      return (
            <div className ="container" style={myStyle}>
                <h1 className='my-3'>About us</h1>
                <div className="accordion accordion-flush" id="accordionFlushExample" style={myStyle}>
                    <div className="accordion-item">
                     <h2 className="accordion-header">
                <button className="accordion-button collapsed" type="button" style={myStyle} data-bs-toggle="collapse" data-bs-target="#flush-collapseOne" aria-expanded="false" aria-controls="flush-collapseOne">
                    Accordion Item #1
                </button>
                </h2>
                <div id="flush-collapseOne" className="accordion-collapse collapse" data-bs-parent="#accordionFlushExample">
                <div className="accordion-body" style={myStyle}>Placeholder content for this accordion, which is intended to demonstrate the <code>.accordion-flush</code> className. This is the first item's accordion body.</div>
                </div>
                     </div>
                <div className="accordion-item"> 
                <h2 className="accordion-header">
                <button className="accordion-button collapsed" type="button" style={myStyle} data-bs-toggle="collapse" data-bs-target="#flush-collapseTwo" aria-expanded="false" aria-controls="flush-collapseTwo">
                    Accordion Item #2
                </button>
                </h2>
                <div id="flush-collapseTwo" className="accordion-collapse collapse" data-bs-parent="#accordionFlushExample">
                <div className="accordion-body" style={myStyle}>Placeholder content for this accordion, which is intended to demonstrate the <code>.accordion-flush</code> className. This is the second item's accordion body. Let's imagine this being filled with some actual content.</div>
                </div>
            </div>
            <div className="accordion-item">
                <h2 className="accordion-header">
                <button className="accordion-button collapsed" type="button" style={myStyle} data-bs-toggle="collapse" data-bs-target="#flush-collapseThree" aria-expanded="false" aria-controls="flush-collapseThree">
                    Accordion Item #3
                </button>
                </h2>
                <div id="flush-collapseThree" className="accordion-collapse collapse" data-bs-parent="#accordionFlushExample">
                <div className="accordion-body" style={myStyle}>Placeholder content for this accordion, which is intended to demonstrate the <code>.accordion-flush</code> className. This is the third item's accordion body. Nothing more exciting happening here in terms of content, but just filling up the space to make it look, at least at first glance, a bit more representative of how this would look in a real-world application.</div>
                </div>
            </div>
                </div>
                <div className="container my-3">
                <button onClick={toggleStyle} type ="button" className="btn btn-primary">{btntext}</button>
                </div>
             </div>
      )
    }
    
    
    // Log to console
    console.log('Hello console')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search