react-props 相关问题

ReactJS是由facebook开发的JS库。道具是反应组件中使用的变量。在对反应道具的使用有疑问时使用标签。


为什么不将DefaultProps应用于我的React功能组件? 我正在从事一个React项目,并试图将DefaultProps与功能组件一起使用。但是,当我不明确地通过道具时,默认值不会被应用。 课程组件:

function Course(props) { return ( <div className={Styles.card}> <img src={props.image} alt="" /> <h2>{props.name}</h2> <p>{props.price}</p> </div> ); } Course.defaultProps = { image: image1, name: "Default Course", price: "1 Like + 1 Subscribe", }; export default Course;

回答 1 投票 0

代码有效,但仍会遇到“绑定元素”计数“计数”的错误,隐含地具有“任何“类型””。如何摆脱这一点?

与Usestate一起工作。我将状态变量和渲染函数传递给我的按钮组件。代码似乎在起作用,但是我会隐含地获得“绑定元素”计数'count'

回答 1 投票 0



通过React Hook形式和Zod

我有孩子ZOD模式,这些模式在许多父母ZOD模式中都重复使用,因此必须重复使用React组件,因为我不想复制代码。

回答 1 投票 0

反应 - 通过道具

目前,这给我一个错误,说“完整的”是不确定的。 todo.iscompleted = todo.iscompleted? false:true; 以上代码是引发错误的原因。 我得到的建议是...

回答 2 投票 0


通过其他组件后位置/城市值未定义,然后渲染页面变为空白..有什么解决方案吗?

我有两个组件“搜索”和“Maindata”。我将输入值从搜索组件传递到 maindata 组件,我想在其中用 i 替换 city 属性...

回答 2 投票 0

如何将 prop 传递到从类导出的数组中,以便该数组使用该 prop 实例化其子组件以进行反应?

问题陈述 我有一个从另一个文件导出的数组。我需要从父类向它传递一个 prop,以便导出的数组使用该 prop 作为值进行实例化。 问题...

回答 1 投票 0

使用组件槽时如何为槽组件 props 编写正确的 TypeScript 类型

我有一个 React 组件,它接受 SlotComponent 属性以及 slotComponentProps 属性,用于传递给插槽组件的属性: 接口 MyComponentProps 我有一个 React 组件,它接受 SlotComponent 属性以及要传递给插槽组件的 slotComponentProps 属性: interface MyComponentProps<T extends React.ElementType> { SlotComponent: T slotComponentProps: React.ComponentProps<T> } function MyComponent<T extends React.ElementType>({ SlotComponent, slotComponentProps, }: MyComponentProps<T>): React.ReactNode { return ( <div> <SlotComponent {...slotComponentProps} /> // Other things go here. </div> ) } 这部分有效,但没有我希望的那么灵活。我遇到的问题是: 如果我尝试将 SlotComponent 设为可选并将其默认为 'button' 之类的内容,我会收到 TypeScript 错误: TS2322: Type '"button"' is not assignable to type 'T'. '"button"' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'ElementType<any, keyof IntrinsicElements>'. SlotComponent = 'button', ~~~~~~~~~~~~~ 如果我尝试将 slotComponentProps 设置为可选,则会收到此 TypeScript 错误: TS2322: Type '{}' is not assignable to type 'LibraryManagedAttributes<T, any>'. <SlotComponent {...slotComponentProps} /> ~~~~~~~~~~~~~ 如果我尝试将 slotComponentProps 设为可选并添加 {} 作为默认值,我会收到此 TypeScript 错误: TS2322: Type '{}' is not assignable to type 'ComponentProps<T>'. slotComponentProps = {}, ~~~~~~~~~~~~~~~~~~ 如何更新我的类型以实现这些更改? 不确定您想要实现哪个级别的灵活性,但我想出了以下解决方案,它允许以下操作: SlotComponent可以跳过。将使用 DEFAULT_ELEMENT_TYPE 代替。 slotComponentProps仍可提供,类型受控。 slotComponentProps可以跳过。 两者都可以跳过 两者都可以提供。检查 slotComponentProps 的类型是否与 SlotComponent 的道具兼容 解决方案是将这两个参数设为可选,并在函数体中默认它们,以防不存在。当未指定 slotComponentProps 时,默认泛型类型用于控制 SlotComponent 的类型。 interface MyComponentProps<T extends React.ElementType> { SlotComponent?: T slotComponentProps?: React.ComponentProps<T> } const DEFAULT_ELEMENT_TYPE = 'button' as const; type DefaultElementTypeType = typeof DEFAULT_ELEMENT_TYPE; function MyComponent<T extends React.ElementType = DefaultElementTypeType>({SlotComponent, slotComponentProps}: MyComponentProps<T>): React.ReactNode { const Component: React.ElementType = SlotComponent ?? DEFAULT_ELEMENT_TYPE; return ( <div> <Component {...slotComponentProps} /> // Other things go here. </div> ) }

回答 1 投票 0

React 19“ref as prop”和 TypeScript

React 19 引入了现有forwardRef 的新替代方案,称为 ref as prop: https://react.dev/blog/2024/12/05/react-19#ref-as-a-prop 但不幸的是,b 中提供的示例...

回答 1 投票 0

Vite+React 项目子组件没有向父组件传递值

我正在开发一个 Vite+React 项目,目前我正在开发移动导航栏,所以我的第一个问题是当我第一次单击移动导航图标时,导航组件正在显示,但在第二个 cli 上...

回答 1 投票 0

无法从父元素接收道具

有一个父组件 Products 和另一个组件 SingleProduct 我试图将产品作为道具传递给 SingleProduct 但当我 console.log 时未定义 谁能告诉我我在做什么...

回答 1 投票 0

如何在react中为按钮添加disabled属性?

这是我的代码 从“反应”导入反应 从“prop-types”导入 PropTypes; 导出默认功能按钮({ htmlType,类型,禁用,操作,...props}){ 返回 ( 这是我的代码 import React from 'react' import PropTypes from "prop-types"; export default function Button({ htmlType, type, disabled, action, ...props}) { return ( <button type={htmlType} onClick={action}> {props.children} </button> ) } Button.propTypes = { htmlType: PropTypes.string.isRequired, action: PropTypes.func, disabled: PropTypes.bool }; 我通过这段代码调用Button组件 <Button disabled={true}>button title</Button> 我想在禁用 props 为 true 时向按钮添加禁用的 html 属性,该怎么做? 您可以像这样编写单行 if-else 语句: <button disabled={propsDisabled}>button title</button> 这里,propsDisabled是可以通过props传递的变量,它是一个boolean变量,可以是true或false。为了避免混淆,我没有使用 disabled 本身,但您可以将变量名称用作 disabled。 当propsDisabled为true时,该按钮将被禁用,而当propsDisabled为false时,该按钮将不会被禁用。 Aya,我无法完全理解你的问题,看起来你正在试图解决一个问题,而你一开始的问题中有第二个问题。 您使用Ant组件吗?在 disabled 组件本身上使用 <Button /> 属性。 (注意组件名称中的大写字母 B Button)。 <Button disabled={true} /> // or (it's the same but different JSX syntax) <Button disabled /> 这与兄弟@Abdul Qadir的回答相同。 如果您使用原生 HTML elements,您还可以在 <button /> 元素上调用禁用的 属性(请注意元素名称 button 中的小字符 b)同样的方式,它应该可以工作: <button disabled={true}>I'm disabled</button> // or (the same but different syntax) <button disabled>I'm disabled</button> 所以这是两个答案, 如果您正在使用 Ant 组件: import { Button } from 'antd'; const CustomButton = ({ disabled, children }) =>{ return <Button disabled={disabled}>{children}</Button> } 如果您使用原生 HTML 元素: const CustomButton = ({ disabled, children }) =>{ return <button disabled={disabled}>{children}</button> }

回答 2 投票 0

React Typescript HOC

我正在尝试实现 HOC,根据用户是否被授权将其重定向到指定路径 从“react-router-dom”导入{Navigate}; 导入 {useAppSelector} f...

回答 1 投票 0

如何将值从 openAPIweather 传递到组件

我正在使用React-Native做一个天气应用程序,现在我正在尝试获取API的数据,因此创建了一个文件WeatherData.js,它返回名称、温度和天气。 我想将数据传递给组件

回答 1 投票 0

如何指定 prop 在自定义 React 组件中采用 ref?

我在 React 中有一个自定义组件,我希望能够保存 ref (即,它应该将 ref 作为其 props 之一)。 (如果我们想将其用作 ToolTip 组件的子组件,这是必需的。)...

回答 1 投票 0

如何避免在 React 组件中钻取 props?

我知道这是一个有点多余的问题,但我在我的 React 应用程序中面临着一个特定的挑战,我希望有人可以帮助解决。我有一个组件需要传递多个 props...

回答 2 投票 0

从 Checkbox 切换后 Switch 组件不工作

我之前使用 Ant Design Checkbox 来检查和发布评论,运行良好,没有错误。 我之前使用 Ant Design Checkbox 来检查和发布评论,运行良好,没有错误。 <Checkbox key={record.id} checked={Publish[record?.id]?.["ispublish"] == 'true' ? true : false} onChange={(e) => Update(e.target.checked, record)} /> 我想用 Switch 代替。 <Switch key={record.id} checked={Publish[record?.id]?.["ispublish"] == 'true' ? true : false} onChange={(e) => Update(e.target.checked, record)} /> 我收到错误: 无法读取未定义的属性(读取“已检查”) 我想要Switch工作。 onChange组件的Switch prop 回调需要 两个参数。第一个是当前 checked 值,第二个是 onChange 事件对象。 查看Switch组件API: 财产 描述 类型 onChange 当选中状态发生变化时触发 function(checked: boolean, event: Event) 与CheckBox组件API相比: 财产 描述 类型 onChange 状态改变时触发的回调函数 (e: CheckboxChangeEvent) => void 更新为使用第一个参数,checked: <Switch key={record.id} checked={Publish[record?.id]?.ispublish == 'true'} onChange={(checked) => Update(checked, record)} />

回答 1 投票 0

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.