javascript - Using React functional component as a Type - Stack Overflow

I am able to use a React class ponent (i.e. React.Component) as a Type but unable to use functionalsta

I am able to use a React class ponent (i.e. React.Component) as a Type but unable to use functional/stateless ponents. Here is an example:

import Footer from 'path/to/ponent/Footer';

interface ComponentProps {
    customFooter: (Footer)
}

class MyComponent extends React.Component<ComponentProps> {
    render() {
        return (
            <div>
                {this.props.customFooter}
            </div>
        );
    }
}

Footer.tsx

const Footer: React.StatelessComponent<{ children?: any }> = ({ children }) => (
    <footer>
        <div>
            {children}
        </div>
    </footer>
);

export default Footer;

The red underline is under the (Footer) and reads: Cannot find name 'Footer'.

I've discovered that if I use a React class ponent instead of a functional ponent, my issue goes away. Why is it that I cannot use a functional ponent and is there a way for me to do so?

I am able to use a React class ponent (i.e. React.Component) as a Type but unable to use functional/stateless ponents. Here is an example:

import Footer from 'path/to/ponent/Footer';

interface ComponentProps {
    customFooter: (Footer)
}

class MyComponent extends React.Component<ComponentProps> {
    render() {
        return (
            <div>
                {this.props.customFooter}
            </div>
        );
    }
}

Footer.tsx

const Footer: React.StatelessComponent<{ children?: any }> = ({ children }) => (
    <footer>
        <div>
            {children}
        </div>
    </footer>
);

export default Footer;

The red underline is under the (Footer) and reads: Cannot find name 'Footer'.

I've discovered that if I use a React class ponent instead of a functional ponent, my issue goes away. Why is it that I cannot use a functional ponent and is there a way for me to do so?

Share Improve this question edited Jul 5, 2018 at 16:32 Erik Philips 54.7k11 gold badges131 silver badges156 bronze badges asked Jul 5, 2018 at 15:20 noblerarenoblerare 12k26 gold badges91 silver badges167 bronze badges 2
  • you have a typo here: <footer}> – Kevin Amiranoff Commented Jul 5, 2018 at 15:27
  • Thanks for the catch. That was a result of me removing unnecessary css class attribute from my question – noblerare Commented Jul 5, 2018 at 15:30
Add a ment  | 

1 Answer 1

Reset to default 5

Footer isn't a type it is only a variable, to get the type you could use typeof

const Footer: React.StatelessComponent<{ children?: any }> = ({ children }) => (
    <footer>
        <div>
            {children}
        </div>
    </footer>
);

interface ComponentProps {
    customFooter: typeof Footer    
}

then you can use it like this:

class MyComponent extends React.Component<ComponentProps> {
    render() {
        const {customFooter: CustomFooter} = this.props;
        return (
            <div>
                {<CustomFooter>footer children</CustomFooter>}
            </div>
        );
    }
}

const MainComponent = () => {
    return (<MyComponent customFooter={Footer}/>)
};

if you want to add an actual jsx element as value like this:

const MainComponent = () => {
    return (<MyComponent customFooter={<Footer>footer children</Footer>}/>)
};

then you need to set the type of customFooter to something like React.ReactNode:

interface ComponentProps {
    customFooter: React.ReactNode
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744216091a4563560.html

相关推荐

  • javascript - Using React functional component as a Type - Stack Overflow

    I am able to use a React class ponent (i.e. React.Component) as a Type but unable to use functionalsta

    9天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信