React antd Button传递参数到自定义方法里

本文介绍了如何在React应用中使用AntDesign库创建一个按钮,点击后调用handleButtonClick函数,参数为每个记录的id。代码展示了如何遍历records数组并为每个记录绑定点击事件。
摘要由CSDN通过智能技术生成

试了几种方式

记录一下比较有效的一种

import { Button } from 'antd';

interface recordClass{
  id: string,
  name: string
}

const records = [];

const handleButtonClick = (id:string) => {
  console.log(id);
}

export default () => {

return (
    <div>
        {records.map((record:recordClass,index) => (
          <div>
            <p>{record.name}</p>
            <Button onClick={(e) => handleButtonClick(record.id)} >Button</Button>
          </div>
        ))}
    </div>
  );
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
对于React Hooks和Ant Design的Form组件,如果你想要在自定义的富文本框中进行输入内容的实时校验,并且在用户输入内容后立即提示消失,可以尝试以下步骤: 1. 首先,确保你的自定义富文本框组件是一个受控组件,即它的值由父组件通过props传递,并且通过onChange事件进行更新。 2. 在父组件中,使用React Hooks的useState来管理输入内容的状态。例如,你可以创建一个名为"content"的状态变量和一个名为"setContent"的状态更新函数。 3. 在自定义富文本框组件中,将输入框的值设置为从父组件传递的"content"变量,并在onChange事件中调用父组件传递的"setContent"函数更新输入内容。 4. 在Ant Design的Form组件中,可以使用getFieldDecorator方法来包装自定义富文本框,并提供校验规则和提示信息。例如,你可以使用rules属性指定校验规则,并使用message属性指定提示信息。 5. 如果你希望在用户输入内容后立即显示提示信息,并且在用户继续输入时立即消失,可以使用validateTrigger属性设置校验触发的时机。例如,你可以将validateTrigger属性设置为"onChange",这样在用户输入时即可触发校验并显示提示信息。 下面是一个示例代码片段,演示了如何实现上述功能: ```jsx import React, { useState } from 'react'; import { Form, Input } from 'antd'; const CustomRichTextBox = ({ content, setContent }) => { const [isTouched, setIsTouched] = useState(false); const handleInputChange = (e) => { setIsTouched(true); setContent(e.target.value); }; return ( <Form.Item label="Content" validateStatus={isTouched && !content ? 'error' : ''} help={isTouched && !content ? 'Content is required' : ''} > <Input value={content} onChange={handleInputChange} /> </Form.Item> ); }; const MyForm = () => { const [content, setContent] = useState(''); const handleSubmit = () => { // 提交表单逻辑 }; return ( <Form onSubmit={handleSubmit}> <CustomRichTextBox content={content} setContent={setContent} /> <Form.Item> <Button type="primary" htmlType="submit"> Submit </Button> </Form.Item> </Form> ); }; ``` 在上述代码中,我们使用了useState来管理输入内容的状态,使用isTouched状态变量来标记是否已经进行过输入操作。在CustomRichTextBox组件中,根据isTouched和content的值来设置校验状态和提示信息。在MyForm组件中,将content状态变量和setContent状态更新函数传递给CustomRichTextBox组件,并在提交表单时使用handleSubmit函数处理逻辑。 希望以上信息能够对你有所帮助!如果还有其他问题,请随时提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值