简体   繁体   English

如何传递带有参数的 function 与 typescript 反应

[英]How to pass a function with parameters in react with typescript

I want to pass a handleClick to the son so I don't have to do it twice.我想将 handleClick 传递给儿子,所以我不必做两次。 The code is the following:代码如下:

Mother:母亲:

const Mother = () => {
    const [selectedOption, setSelectedOption] = useState<'rooms' | 'questions' | 'notifications'>('rooms')

    const customSectionStyle = 'bg-primary-500 text-white hover:bg-primary-600'

    //handle click
    const handleClick = (href, section) => {
      setSelectedOption(section);
      router.push(href)
    }
    return(
        <>
            <Son onClick={() => handleClick} selectedOption={selectedOption} customSectionStyle={customSectionStyle}/>
        </>
    )
}

Son:儿子:

const Son: React.FC<{ selectedOption?: string | '', onClick?: (href: string, section: string) => void | void, customSectionStyle?: string | '' }> = (selectedOption, onClick, customSectionStyle) => {
    return(
        <>
            <MyComponent onClick={() => {onClick('/','home')}} customStyle={selectedOption === 'rooms' ? customSectionStyle : ''}/>
            <MyComponent onClick={() => {onClick('/','settings')}} customStyle={selectedOption === 'settings' ? customSectionStyle : ''}/>
        //...
        </>
    )
}

Basically this sets the background color in case an option is selected or not.基本上,这会设置背景颜色,以防选择或不选择选项。 MyComponent is a component that takes that props (and/or others) and sets its own content. MyComponent是一个组件,它采用该道具(和/或其他)并设置自己的内容。

This throws the type error _onClick is not a function .这会引发类型错误_onClick is not a function I believe I didn't do it properly.. What did I miss, in your opinion?我相信我没有正确地做到这一点。在你看来,我错过了什么?

const Son: React.FC<{ selectedOption?: string | '', onClick?: (href: string, section: string) => void | void, customSectionStyle?: string | '' }> = (selectedOption, onClick, customSectionStyle)

Replace with用。。。来代替

const Son: React.FC<{ selectedOption?: string, onClick?: (href: string, section: string) => void, customSectionStyle?: string }> = ({ selectedOption, onClick, customSectionStyle })

Son Will be called with all props that you've to Destructur it or directly read from props儿子将被调用所有你必须破坏它的道具或直接从道具中读取

(selectedOption, onClick, customSectionStyle) should be ({selectedOption, onClick, customSectionStyle}) (selectedOption, onClick, customSectionStyle) 应该是 ({selectedOption, onClick, customSectionStyle})

 const Son: React.FC<{ selectedOption?: string | '', onClick?: (href: string, section: string) => void | void, customSectionStyle?: string | '' }> = ({selectedOption, onClick, customSectionStyle}) => { return( <MyComponent onClick={() => {onClick('/','home')}} customStyle={selectedOption === 'rooms'? customSectionStyle: ''}/> <MyComponent onClick={() => {onClick('/','settings')}} customStyle={selectedOption === 'settings'? customSectionStyle: ''}/> //... ) }

are you sure about returning two adjacent MyComponent over there?你确定要在那里返回两个相邻的 MyComponent 吗? And I think that you miss the curly braces when getting the props in the son component definition =而且我认为您在获取子组件定义中的道具时会错过花括号=

({ selectedOption, onClick, customSectionStyle }) ({ selectedOption, onClick, customSectionStyle })

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

相关问题 Typescript/JavaScript - 如何将 rest 参数作为参数而不是数组传递给后续的 function? - Typescript/JavaScript - How to pass rest parameters to subsequent function as parameters and not as array? 如何将0传递给TypeScript可选参数? - How to pass 0 to TypeScript optional parameters? Typescript - 将接口文字传递给函数参数 - Typescript - Pass interface literals to function parameters 如何将道具传递给 function 并使用 react 和 typescript 在组件中访问它? - How to pass props to a function and access it in a component using react and typescript? 如何在不创建新函数的情况下将参数传递给React + Typescript中的回调? - How to pass arguments to callback in React + Typescript without new function creation? 如何将接口/类型动态传递给打字稿中的常见反应函数 - How to pass interface/type dynamically to a common react function in typescript 如何将参数传递给函数 - How to pass parameters to a function 如何在 React Native 中使用上下文 API + Hooks 传递 function 的参数 - How to pass parameters of a function using Context API + Hooks in React Native 如何将回调函数参数传递给父反应组件? - How to pass callback function parameters to parent react component? 如何在react-editext的&#39;onSave()&#39;函数中传递两个参数? - How to pass two parameters in the 'onSave ()' function in react-editext?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM