【问题标题】:how to pass props to child in tsx?如何在 tsx 中将道具传递给孩子?
【发布时间】:2019-05-24 05:12:38
【问题描述】:

当我将道具传递给孩子时,我有一个错误“属性‘isClicked’在类型‘IntrinsicAttributes & IntrinsicClassAttributes’上不存在” 我写了“isClicked?:布尔值”。我还应该做什么?

export interface DropDownProperties extends ComponentBaseProperties {
  multiSelect?: boolean;
  IconTextColor?:string;
  isClicked?: boolean;
}

export interface DropDownState extends ComponentBaseState {
  dropDownOptions: DropDownItem[];
  isOpen: boolean;
  results: string[];
  isClicked?: boolean;
 
}
export default class DropDown extends ComponentBase<
  DropDownProperties,
  DropDownState
> { return ( <DropDownItem
          iconName={option.iconName}
          value={option.value}
          displayValue={option.displayValue ? true : false}
          key={option.name}
          onClick={(e) => this.optionSelected()}
          isClicked={this.state.isOpen}
        >
          {option.props.children}
        </DropDownItem>
      ))}
       </ul>
      );
    }
  };

【问题讨论】:

  • 您的问题似乎不完整,请您详细说明您的问题是什么?
  • 什么是 DropDownItem?在上面的代码中,DropDown 被输入了 isClicked 属性。不是DropDownItem

标签: reactjs typescript tsx


【解决方案1】:

如果您在组件中使用连接,使用下面的代码应该可以解决问题。

export default connect<{}, {}, Props>(..........)

这里connect有3个参数,第三个参数是接口Props,里面有你要输入的变量。

【讨论】:

  • 只有连接的方法吗?我没用,项目里没有Redux
  • 不,不需要redux。
  • 既然是txs,就定义一个接口,里面有propisClicked。使用接口扩展子组件类,如 class ComponentName extends React.Component {} 如有必要,添加构造函数,如 constructor(props, state) { super(props, state);这应该可以解决问题
猜你喜欢
  • 2020-08-08
  • 1970-01-01
  • 2018-04-09
  • 2016-03-19
  • 1970-01-01
  • 1970-01-01
  • 2017-06-03
  • 1970-01-01
  • 2019-04-04
相关资源
最近更新 更多