简体   繁体   English

打字稿孩子与父母有相同的道具,有没有更干净的方法来做到这一点?

[英]Typescript child has same props as parent, is there a cleaner way to do it?

Quiz question component prop types :测验问题组件道具类型:

type Props = {

    question_details: question_details,
    handle_back_click: (event: React.MouseEvent<HTMLButtonElement>) => void
    handle_next_click: (event: React.MouseEvent<HTMLButtonElement>) => void
    question_number:number

}

It renders a child component QuestionButtons like so它像这样呈现一个子组件 QuestionButtons

   <QuestionButtons

        answer_selected={answer_selected}
        handle_back_click={handle_back_click}
        handle_next_click={(e)=> handle_next(e)}
        question_number={question_number}

    />

Which also has the same props哪个也有同样的道具

type Props = {

    answer_selected: string,
    handle_back_click: (event: React.MouseEvent<HTMLButtonElement>) => void
    handle_next_click: (event: React.MouseEvent<HTMLButtonElement>) => void
    question_number:number

}

My question is, is there a cleaner way to do this, so I do not repeat the props, without using redux or moving the QuestionButtons component into the parent quiz question component我的问题是,有没有更简洁的方法来做到这一点,所以我不重复道具,而不使用 redux 或将 QuestionButtons 组件移动到父测验问题组件中

You can pass rest props:您可以传递休息道具:

   <QuestionButtons
        answer_selected={answer_selected}
        {...props}
    />

which props is those that are same.哪些props是相同的。

You can use the Javascript spread ... operator to do it.您可以使用 Javascript spread ...运算符来执行此操作。

<QuestionButtons
  answer_selected={an swer_selected}
  { ...questionProps }
/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法使用 Typescript 使可选的 React 道具类型更清洁? - Is there a way to make optional React props types cleaner with Typescript? React:父母发送的道具与孩子收到的道具不同 - React: Props sent by parent are not the same received by child 将 React-Typescript 中的 Function 道具从父级传递给子级 - Passing Function Props in React-Typescript from parent to child Typescript 在道具中获取儿童道具 - Typescript Getting Child Props in Props 每个父子对在 D3 Hierarchy Typescript 中具有相同的颜色 - Each parent-child pair has same color in D3 Hierarchy Typescript 反应:将 2 个道具从子组件传回同一 function 中的父组件 - React: Pass 2 props back to the parent component in the same function from the child 如何一次(同时)更新父 state 和子组件道具 - How to update parent state and child components props at once (at the same time) 从子接收道具到父反应组件的最佳方式 - Best way to recieve props from child to parent react component 有没有办法在没有 onClick 的情况下将道具从孩子传递给父母 - Is there a way of passing props from child to parent without onClick 更清洁的方式来编写element.parent()。parent()。parent()。parent()。parent() - Cleaner way to write element.parent().parent().parent().parent().parent()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM