React Js 中的函数组件在哪里获取 props 和设置 props?

发布于 2025-01-16 02:51:17 字数 301 浏览 4 评论 0 原文

props data coming from the parent component

const [state, setState] = useState({

reportTypesVal: { label: "", value: "" } || props?.reportTypesVal});

为什么我无法将这样的数据设置为reportTypesVal? 如果我尝试在 useEffect 中设置数据,它不会将值设置为 reportTypesVal。 我怎样才能做到这一点?

props data coming from the parent component

const [state, setState] = useState({

reportTypesVal: { label: "", value: "" } || props?.reportTypesVal});

Why I'm unable to set data like this to reportTypesVal?
If I try to set data in useEffect It doesn't set the value to reportTypesVal.
How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

噩梦成真你也成魔 2025-01-23 02:51:17

你的问题有点不清楚,但从标题来看:

React Js 中的函数组件在哪里获取 props 和设置 props?

我假设您问如何传递道具,这是新测试版文档的示例: https://beta.reactjs.org/learn/passing-props-to-a-component

Your question is a bit unclear, but from the title:

Where to get props and set props in a functional component in React Js?

I assume you're asking how to pass props, here is an example on the new beta documentation: https://beta.reactjs.org/learn/passing-props-to-a-component

时光瘦了 2025-01-23 02:51:17

您应该在子名称函数中实现它。

前任:

import ...
import ...

function showSomething( {MyProps1, MyProps2} )  // <---- here
{
   useEffect(() => {
        
        console.log(MyProps1);      

   }
...
}

You should implement it in the child name function.

EX:

import ...
import ...

function showSomething( {MyProps1, MyProps2} )  // <---- here
{
   useEffect(() => {
        
        console.log(MyProps1);      

   }
...
}
旧瑾黎汐 2025-01-23 02:51:17

这是将 props 传递到组件中的方式:

<Component property={'randomText'} />

这是在组件中检索它的方式:

function Component (props) = {
  return (
    <p>{props.property}</p>
  )
}

您可以通过使用状态并将 set 函数作为属性传递来更改组件内部的属性:

const [randomText, setRandomText] = React.useState('randomText')
return(
    <Component property={randomText} propertySet={setRandomText} />
)

在组件内部您可以执行以下操作:

function Component (props) = {
      return (
        <button onClick={() => {props.setRandomText('randomText2')}}>{props.randomText}</button>
      )
    }

然后单击该按钮它将更改为“randomText2”

This is how you pass props into a component:

<Component property={'randomText'} />

This is how you retrieve it in the component:

function Component (props) = {
  return (
    <p>{props.property}</p>
  )
}

You can change the property inside of the component by using states and passing the set function as a property:

const [randomText, setRandomText] = React.useState('randomText')
return(
    <Component property={randomText} propertySet={setRandomText} />
)

Inside the component you can do:

function Component (props) = {
      return (
        <button onClick={() => {props.setRandomText('randomText2')}}>{props.randomText}</button>
      )
    }

Then when you click the button it will change to 'randomText2'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文