简体   繁体   English

如何从 react hooks 或 useEffect 获取新的更新数据?

[英]How do I get the new updated data from react hooks or useEffect?

How do I get the newly updated data from react hooks or useEffect?如何从 react hooks 或 useEffect 获取新更新的数据?

I loaded the parent components with the title: Shoes then change the input to "New Shoes" but the data inside my child components doesn't change, its still "Shoes" .我用标题加载了父组件: Shoes然后将输入更改为"New Shoes"但我的子组件中的数据没有改变,它仍然是"Shoes" how do I get it updated?如何更新?

in the parent components:在父组件中:

<UrlInput
   value={url}
   title={title}
   baseURL={baseURL}
   onChange={this.onURLChange}
/>

Child component子组件

const UrlInput = ({title}) => {
    const [newTitle, setTitle] = useState(title);

     useEffect(() => {
         console.log(title)
         setTitle(newTitle)
     })

  return (

  );
};

You should add title as a dependency to useEffect and update state with title and not newTitle .您应该将 title 作为依赖项添加到 useEffect 并使用title而不是newTitle Also the original state needs to be set with title and not Title .此外,原始 state 需要设置为title而不是Title

const UrlInput = ({title}) => {
    const [newTitle, setTitle] = useState(title);

     useEffect(() => {
         console.log(title)
         setTitle(title)
     }, [title]);

  return (

  );
};

PS Also its an antipattern to copy props into state when state is directly derivable from props unless you need to modify the state locally. PS 此外,当 state 可以直接从 props 派生时,将 props 复制到 state 是一种反模式,除非您需要在本地修改 state。

const UrlInput = ({title, ...rest}) => {
    const [newTitle, setTitle] = useState(title);

     useEffect(() => {
         console.log(title)
         setTitle(title)
     },[title]) 

  return (

  );
};

try replacing the code with the above one.You should pass a new value into setTitle() you were passing the same variable that stored the previous value.尝试用上面的代码替换代码。您应该将一个新值传递给 setTitle() 您传递的变量与存储先前值的变量相同。

For the get, any state-specific change in a component use the useEffect(() => {},[specific condition here]).对于 get,组件中任何特定于状态的更改都使用 useEffect(() => {},[这里的特定条件])。

You can follow this code here.您可以在此处遵循此代码。

in the parent components:在父组件中:

const [title, setTitle] = useState('');  // initial state parent 
const onURLChange = e => setTitle(e.target.value) ; set state parent from children triger 

<UrlInput
    value={url}
    title={title}
    baseURL={baseURL}
    onChange={this.onURLChange}
/>

Child component子组件

const UrlInput =props => {
    const [newTitle, setTitle] = useState(props.Title);

     useEffect(() => {
         console.log(title)
         setTitle(newTitle)
     },[props.Title])

  return (

  );
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将它变成 React Hooks for useEffect()? - How do I turn this into React Hooks for useEffect()? 我如何正确使用 useEffect 进行异步获取调用和反应? 反应钩子/详尽的deps - How do i properly use useEffect for a async fetch call with react? react-hooks/exhaustive-deps 如何在 useEffect 中使用更新的值 - How do I use an updated value in useEffect 使用 React 中的 useEffect 和 useState 钩子从获取请求中管理数组 - Manage array from get request using useEffect and useState hooks in React 如何设置useEffect来首先使用eslint-react-hooks从API获取数据? - how to set an useEffect to fetch data from API at first render with eslint-react-hooks? 如何在每次渲染之前重置 UseEffect React Hooks 中的数据? - How to reset data in UseEffect React Hooks before each rendering? 如何在 React function 组件中不使用 useEffect 钩子来获取数据? - How to fetch data without useEffect hooks in React function component? 如何编写一个连接来自其他 React Hook 的异步数据的 React Hook - How do I write a React Hook that Concatenates asynchronous data from other React Hooks React Hooks &amp; UseEffect 不使用 socketIO 数据更新显示 - React Hooks & UseEffect not updating display with socketIO data React Hooks:在 useEffect 警告中获取数据 - React Hooks : fetch data inside useEffect warning
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM