Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is this Cannot read properties of null (reading 'useState')?

import React from 'react'
import './App.css'
import { Description } from './Description'
/**
 *
 * @type {React.FC}
 */
export const App = () => {
  return (
    <div>
      <div className="dogdisplay">
        <Description />
      </div>
    </div>
  )
}
import React, { useState } from 'react'
import { DogImage } from './DogImage'
const [dogUrl, setDogUrl] = useState(
  'https://images.dog.ceo/breeds/spaniel-brittany/n02101388_6057.jpg',
)
const API_URL = 'https://dog.ceo/api/breeds/image/random'

export const Description = () => {
  return (
    <div>
      <title>React.com</title>
      <p>これは犬のサイトです。</p>
      <img src={dogUrl} />
      <button onClick={() => setDogUrl(<DogImage url={API_URL} />)}>
        update
      </button>
    </div>
  )
}

export const DogImage = props => {
  fetch(this.props.url)
    .then(res => {
      res.json()
    })
    .then(data => {
      return data.message
    })
}

What is this Cannot read properties of null (reading ‘useState’)?

Cannot read properties of null (reading ‘useState’)?
error.
I want the dog image to change when I press the refresh button and then the refresh button.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Hooks can only be called inside of the body of a function component.

import React, {useState} from 'react'
import { DogImage } from './DogImage'

const API_URL = 'https://dog.ceo/api/breeds/image/random'

export const Description = () => {
  const [dogUrl, setDogUrl] = useState(
  'https://images.dog.ceo/breeds/spaniel-brittany/n02101388_6057.jpg',
  )
  return (
    <div>
      <title>React.com</title>
      <p>これは犬のサイトです。</p>
      <img src={dogUrl} />
          <button onClick={() => setDogUrl(<DogImage url={API_URL} />)}>
        update
      </button>
    </div>
  )
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading