简体   繁体   English

如何获取 function 中传递给另一个组件的 useState 变量的值

[英]How to get value of useState variable in function that passed to another component

The best way to describe question is my code:描述问题的最佳方式是我的代码:

function EstateParamsList({ estateType, category }) {
  const [isLoading, setIsLoading] = useState(true)
  const [params, setParams] = useState({})
  const [showPopUp, setShowPopUp] = useState(false)

  useEffect(() => {
    if (category && typeof category.id !== undefined) {
      return db.collection(`dictionaries/ESTATE_PARAMS/${estateType}/${category.id}/params`).onSnapshot(response => {
        const paramsObject = {}
        response.forEach(param => {
          paramsObject[param.id] = {
            ...convertParamObjetcToFieldsConfig(param.data()),
            change: fieldChangedHandler
          }
        })
        setParams(paramsObject)
        setIsLoading(false)
      })
    } else {
      setIsLoading(false)
    }
  }, [category])
 console.log(params)
  const fieldChangedHandler = (event, fieldIdentifier) => {
    if(params)
      console.log(params)
  }

So i have params variable, that im init with object, that i'm getting async from firebase.所以我有参数变量,我用 object 初始化,我从 firebase 获得异步。 Implementation of initializing you can see in useEffect method .您可以在useEffect 方法中看到初始化的实现。 For every object i want to pass ref for the function "fieldChangedHandler", for managing value of inputs.对于每个 object,我想传递 function“fieldChangedHandler”的 ref,用于管理输入值。

fieldChangedHandler is a method of my EstateParamsList . fieldChangedHandler是我的EstateParamsList的一种方法。 But there i cant get value of params!但是我无法获得参数的价值

Question is WHY?问题是为什么? I'm calling fieldChangedHandler only after everything was rendered, and async request was done.只有在所有内容都渲染完毕并且异步请求完成后,我才调用 fieldChangedHandler。

Below is console log of params.下面是参数的控制台日志。 Why in func is empty params ?为什么 func 中的参数是空的?

在此处输入图像描述

Calling:来电:

const renderParamsAsFields = params => {
  const fields = []
  for (const key in params) {
    fields.push(<Field {...params[key]} changed={event => params[key].change(event, key)} />)
  }
  return fields.length ? fields : <div className='EstateParamsManager-EmptyValues'>Нет параметров</div>
}

Why not use curried function?为什么不使用咖喱 function?

const createFieldChangedHandler = (
  params,
  fieldIdentifier
) => event => {
  if (params) console.log(params);
};

function EstateParamsList({ estateType, category }) {
//... other code
  useEffect(() => {
    //...other code
    change: createFieldChangedHandler(
       paramsObject,
       param.id
    ),
    //...other code
   }, [category, estateType]);//fixed missing dependency

When you call the change function you should already have the right values in scope:当您调用更改 function 时,您应该已经在 scope 中拥有正确的值:

<Field {...params[key]} changed={params[key].change} />

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

相关问题 无法在 function 中获取 Usestate 的值 - Impossible to get the value of an Usestate in a function ReactJS - 如何将 useState 变量设置为函数的返回值 - ReactJS - How to set useState variable to the return value of a function 如何从另一个组件 scope 中的组件中的 useState 声明变量中检索内容 - How can I retrieve stuff from an useState declared variable in a component within another component scope 在通过道具传递的另一个组件中获取 url 的 id 值 - vue - get id value of url in another component passed via props - vue 如何将 useState 值从一个组件传递到另一个组件 - How can I pass useState value from one component to another component 如何将选择值传递给另一个函数,并根据传递的值从Json获取数据? - How can I pass select value to another function and get data from Json according to passed value? 如何在超时 function 内获取更新的 useState() 值? - How to get updated useState() value inside timeout function? 如何将 useState state 传递给另一个组件? - How to pass useState state to another component? 如何在一个页面上获取JS变量值传递回PHP中的另一个页面 - How to get JS variable value on one page passed back to another page in PHP 如何将组件的值传递给另一个组件 - How to get value of a component to another component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM