Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.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
Reactjs 添加道具的高阶组件的子组件的类型脚本_Reactjs_Typescript - Fatal编程技术网

Reactjs 添加道具的高阶组件的子组件的类型脚本

Reactjs 添加道具的高阶组件的子组件的类型脚本,reactjs,typescript,Reactjs,Typescript,如何为按钮组件(如下)添加类型脚本,以便typescript知道递增功能已被计数器注入 我知道渲染道具模式和挂钩。我正在寻找一种方法来键入子组件,这些子组件通过cloneElement接收父组件提供的道具 我感觉它与条件类型有关,但不确定如何应用它。这可能吗 从“React”导入React; 常量按钮=({increment})=>( 增量 ); 常量计数器=({children})=>{ const[count,setCount]=React.useState(0); 常量增量=()=>set

如何为
按钮
组件(如下)添加类型脚本,以便typescript知道
递增
功能已被
计数器注入

我知道渲染道具模式和挂钩。我正在寻找一种方法来键入子组件,这些子组件通过
cloneElement
接收父组件提供的道具

我感觉它与条件类型有关,但不确定如何应用它。这可能吗

从“React”导入React;
常量按钮=({increment})=>(
增量
);
常量计数器=({children})=>{
const[count,setCount]=React.useState(0);
常量增量=()=>setCount(计数+1)
返回(
计数:{count}
{React.cloneElement(子元素,{increment}}
);
}
常量应用=()=>(
增量
);
ReactDOM.render(,document.getElementById('root'));

不幸的是,TypeScript处理JSX子级类型的方式并不好,cloneElement也是如此。在您的情况下,
按钮
必须有一个可选的
增量
道具,这意味着您以后必须检查它是否存在

我建议采用以下模式:

import React, { ComponentType } from 'react';
import ReactDOM from 'react-dom';

interface InjectedCounterProps {
  increment: () => void;
}

interface ButtonOwnProps {
  foo: string;
}

type ButtonProps = ButtonOwnProps & InjectedCounterProps;

const Button = ({ increment }: ButtonProps) => (
  <button onClick={increment}>increment</button>
);

const createCounter = <P extends {}>(
  Component: ComponentType<P & InjectedCounterProps>
) => (props: P) => {
  const [count, setCount] = React.useState(0);
  const increment = () => setCount(count + 1);

  return (
    <div>
      count: {count}
      <Component {...props} increment={increment} />
    </div>
  );
};

const Counter = createCounter<ButtonOwnProps>(Button);

const App = () => <Counter foo="bar" />;

ReactDOM.render(<App />, document.getElementById('root'));

import React,{ComponentType}来自'React';
从“react dom”导入react dom;
界面注入反程序{
增量:()=>void;
}
界面按钮打开{
foo:string;
}
类型ButtonProps=ButtonProps&注入式反向旋转;
常量按钮=({increment}:按钮操作)=>(
增量
);
const createCounter=

( 成分:成分类型 )=>(道具:P)=>{ const[count,setCount]=React.useState(0); 常量增量=()=>setCount(count+1); 返回( 计数:{count} ); }; 常量计数器=创建计数器(按钮); 常量应用=()=>; ReactDOM.render(,document.getElementById('root'));

在这里,我们使用HOC从提供的
按钮
组件构造组件,并且我们能够将此按钮的类型约束到HOC注入的类型

这还允许应用于返回组件的道具由我们提供给它的组件定义。这是通过泛型
P
类型参数完成的,并允许我们传递
按钮nprops
,强制在呈现最终
计数器时必须提供
foo


如果在呈现
计数器时删除
foo=“bar”
,,您会注意到我们会得到一个类型错误。不幸的是,TypeScript处理JSX子级类型的方式并不好,而
cloneElement
也是如此。在您的情况下,
按钮
必须有一个可选的
增量
道具,这意味着您以后必须检查它是否存在

我建议采用以下模式:

import React, { ComponentType } from 'react';
import ReactDOM from 'react-dom';

interface InjectedCounterProps {
  increment: () => void;
}

interface ButtonOwnProps {
  foo: string;
}

type ButtonProps = ButtonOwnProps & InjectedCounterProps;

const Button = ({ increment }: ButtonProps) => (
  <button onClick={increment}>increment</button>
);

const createCounter = <P extends {}>(
  Component: ComponentType<P & InjectedCounterProps>
) => (props: P) => {
  const [count, setCount] = React.useState(0);
  const increment = () => setCount(count + 1);

  return (
    <div>
      count: {count}
      <Component {...props} increment={increment} />
    </div>
  );
};

const Counter = createCounter<ButtonOwnProps>(Button);

const App = () => <Counter foo="bar" />;

ReactDOM.render(<App />, document.getElementById('root'));

import React,{ComponentType}来自'React';
从“react dom”导入react dom;
界面注入反程序{
增量:()=>void;
}
界面按钮打开{
foo:string;
}
类型ButtonProps=ButtonProps&注入式反向旋转;
常量按钮=({increment}:按钮操作)=>(
增量
);
const createCounter=

( 成分:成分类型 )=>(道具:P)=>{ const[count,setCount]=React.useState(0); 常量增量=()=>setCount(count+1); 返回( 计数:{count} ); }; 常量计数器=创建计数器(按钮); 常量应用=()=>; ReactDOM.render(,document.getElementById('root'));

在这里,我们使用HOC从提供的
按钮
组件构造组件,并且我们能够将此按钮的类型约束到HOC注入的类型

这还允许应用于返回组件的道具由我们提供给它的组件定义。这是通过泛型
P
类型参数完成的,并允许我们传递
按钮nprops
,强制在呈现最终
计数器时必须提供
foo


如果在呈现
计数器时删除
foo=“bar”

try:{increment}:any{children}:anytry:{increment}:any{children}:any