Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何按照react typescript中的要求标记道具?_Javascript_Reactjs_React Props - Fatal编程技术网

Javascript 如何按照react typescript中的要求标记道具?

Javascript 如何按照react typescript中的要求标记道具?,javascript,reactjs,react-props,Javascript,Reactjs,React Props,我正在尝试将一个道具标记为react typescript应用程序中的必需道具,如何才能做到这一点? 使用react with js,需要在类型上添加关键字。我怎样才能用ts做这个 // Here is my typescript code: interface Props { /** Message to display */ message: string; } const defaultProps: Props = { message: 'World', }; /** My

我正在尝试将一个道具标记为react typescript应用程序中的必需道具,如何才能做到这一点? 使用react with js,需要在类型上添加
关键字。我怎样才能用ts做这个

// Here is my typescript code:

interface Props {
  /** Message to display */
  message: string;
}

const defaultProps: Props = {
  message: 'World',
};
/** My first reusable component */
function HelloWorld({ message }: Props) {
  return <div>Hello {message}</div>;
}

HelloWorld.defaultProps = defaultProps;
//这是我的typescript代码:
界面道具{
/**要显示的消息*/
消息:字符串;
}
const defaultProps:Props={
信息:“世界”,
};
/**我的第一个可重用组件*/
函数HelloWorld({message}:Props){
返回Hello{message};
}
HelloWorld.defaultProps=defaultProps;
这是我试图复制的jsx

import PropTypes from 'prop-types';

function HelloWorld({message}) {
  return <div>Hello {message}</div>
}

HelloWorld.propTypes = {
  message: PropTypes.string.required
};

HelloWorld.defaultProps = {
  message: 'World'
};

export default HelloWorld;
从“道具类型”导入道具类型;
函数HelloWorld({message}){
返回Hello{message}
}
HelloWorld.propTypes={
消息:PropTypes.string.required
};
HelloWorld.defaultProps={
信息:“世界”
};
导出默认HelloWorld;

我认为您必须明确指定
HelloWorld
函数的输出类型

function HelloWorld({ message }: Props): React.SFC<Props> {
  return <div>Hello {message}</div>;
}
函数HelloWorld({message}:Props):React.SFC{
返回Hello{message};
}

const HelloWorld:React.SFC=({message})=>{
返回Hello{message};
}

我真的不明白你的意思?如果定义的接口没有可选字段,默认情况下所有字段都是必需的?您不需要额外的类型检查。

这正是我的想法,顺便说一句,但我不确定。鉴于您也有同样的想法,我认为我已经将道具标记为所需,在界面中不将其设置为可选道具,这实际上是正确的。你需要交一个
React.SFC
返回类型。我不知道什么是SFC,但我会查一下。谢谢你的回答,我不知道什么是SFC,但现在让我查一下
const HelloWorld: React.SFC<Props> = ({ message }) => {
  return <div>Hello {message}</div>;
}