相关疑难解决方法(0)

React中的useState()是什么?

我目前正在学习React中的钩子概念,并试图理解下面的例子.

import { useState } from 'react';

function Example() {
    // Declare a new state variable, which we'll call "count"
    const [count, setCount] = useState(0);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}
Run Code Online (Sandbox Code Playgroud)

上面的示例在处理程序函数参数本身上递增计数器.如果我想修改事件处理函数内的计数值怎么办?

考虑下面的例子

setCount = () => {
  //how can I modify count value here. Not sure if I can use setState to modify its value
  //also I want to modify other state values as well here. …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs react-native react-hooks

51
推荐指数
8
解决办法
5万
查看次数

如何在reactjs中创建api调用new hook api并在组件之间共享?

如何在新的reactjs钩子api中调用restApi.并使用useEffects和useState重用数据?我想只重用组件之间的数据.

import { useState, useEffect } from "react";
export default function getAdvice(url) {
    fetch(`http://api.adviceslip.com/advice`)
        .then(res => res.json())
        .then(res => console.log(res))
        .catch(err => console.log(err)
}
Run Code Online (Sandbox Code Playgroud)

javascript reactjs react-hooks

3
推荐指数
1
解决办法
682
查看次数

标签 统计

javascript ×2

react-hooks ×2

reactjs ×2

react-native ×1