首页 > 其他 > 详细

xml ui - fetch data examples

时间:2020-10-26 14:46:21      阅读:27      评论:0      收藏:0      [点我收藏+]
() => {
  const stringifyData = data => JSON.stringify(data, null, 2)
  const initialData = stringifyData({ data: null })
  const [data, setData] = useState(initialData)

  useEffect(() => {
    const fetchData = () => {
      const uri = ‘https://randomuser.me/api/‘
      fetch(uri)
        .then(res => res.json())
        .then(({ results }) => {
          const { name, gender, dob } = results[0]
          const dataVal = stringifyData({
            ...name,
            gender,
            age: dob.age
          })
          setData(dataVal)
        })
    }

    fetchData()
  }, [])

  return (
    <>
      <h4> ??User Data ??</h4>
      <section>
        <pre>{data}</pre>
      </section>
    </>
  )
}
() => {
  const stringifyData = data => JSON.stringify(data, null, 2)
  const initialData = stringifyData({ data: null })
  const [data, setData] = useState(initialData)

  const [gender, setGender] = useState(‘female‘)

  useEffect(() => {
    const fetchData = () => {
      const uri = ‘https://randomuser.me/api/?gender=‘ + gender
      fetch(uri)
        .then(res => res.json())
        .then(({ results }) => {
          const { name, gender, dob } = results[0]
          const dataVal = stringifyData({
            ...name,
            gender,
            age: dob.age
          })
          setData(dataVal)
        })
    }

    fetchData()
  }, [gender])

  return (
    <>
      <p>Click the buttons below to fetch data</p>
      <button
        onClick={() => setGender(‘male‘)}
        style={{ outline: gender === ‘male‘ ? ‘1px solid‘ : 0 }}
      >
        Fetch Male User
      </button>
      <button
        onClick={() => setGender(‘female‘)}
        style={{ outline: gender === ‘female‘ ? ‘1px solid‘ : 0 }}
      >
        Fetch Female User
      </button>

      <section>
        <pre>{data}</pre>
      </section>
    </>
  )
}
() => {
  const stringifyData = data => JSON.stringify(data, null, 2)
  const initialData = stringifyData({ data: null })
  const loadingData = stringifyData({ data: ‘loading...‘ })
  const [data, setData] = useState(initialData)

  const [gender, setGender] = useState(‘female‘)
  const [loading, setLoading] = useState(false)

  useEffect(
    () => {
      const fetchData = () => {
        setLoading(true)
        const uri = ‘https://randomuser.me/api/?gender=‘ + gender
        fetch(uri)
          .then(res => res.json())
          .then(({ results }) => {
            setLoading(false)
            const { name, gender, dob } = results[0]
            const dataVal = stringifyData({
              ...name,
              gender,
              age: dob.age
            })
            setData(dataVal)
          })
      }

      fetchData()
    },
    [gender]
  )

  return (
    <>
      <p>Click the buttons below to fetch data</p>
      <button
        onClick={() => setGender(‘male‘)}
        style={{ outline: gender === ‘male‘ ? ‘1px solid‘ : 0 }}
      >
        Fetch Male User
      </button>
      <button
        onClick={() => setGender(‘female‘)}
        style={{ outline: gender === ‘female‘ ? ‘1px solid‘ : 0 }}
      >
        Fetch Female User
      </button>

      <section>
        {loading ? <pre>{loadingData}</pre> : <pre>{data}</pre>}
      </section>
    </>
  )
}

https://react-hooks-cheatsheet.com/

xml ui - fetch data examples

原文:https://www.cnblogs.com/Searchor/p/13877975.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!