Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6214

How to specify (optional) default props with TypeScript for stateless, functional React components?

$
0
0

I'm trying to create a stateless React component with optional props and defaultProps in Typescript (for a React Native project). This is trivial with vanilla JS, but I'm stumped as to how to achieve it in TypeScript.

With the following code:

import React, { Component } from 'react';import { Text } from 'react-native';interface TestProps {    title?: string,    name?: string}const defaultProps: TestProps = {    title: 'Mr',    name: 'McGee'}const Test = (props = defaultProps) => (<Text>        {props.title} {props.name}</Text>);export default Test;

Calling <Test title="Sir" name="Lancelot" /> renders "Sir Lancelot" as expected, but <Test /> results in nothing, when it should output "Mr McGee".

Any help is greatly appreciated.


Viewing all articles
Browse latest Browse all 6214

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>