ReactJS 中的“...道具”是什么意思?

JuM*_*Gar 3 reactjs

什么意思……道具?代码是:

export default class NavItem extends React.Component {
constructor() {
    super();
}

render() {
    const { router, index, to, children, ...props } = this.props;
    let isActive = false;

    if (router.isActive('./', true) && index) {
        isActive = true
Run Code Online (Sandbox Code Playgroud)

children我想那是实际元素的孩子,但这...props是什么意思?

谢谢你。

Mic*_*per 6

...在此上下文中使用的称为“其余”参数,如“其余部分”

这一行使用对象解构赋值语法从对象中分配this.props新变量routerindextochildren以及其他所有变量(其余的)this.propsprops

const { router, index, to, children, ...props } = this.props;
Run Code Online (Sandbox Code Playgroud)

这是它用法的另一个示例:

const bigBird = {height: 'tall', color: 'yellow', disposition: 'sunny'};
const {height, ...others} = bigBird;
//height == 'tall'
//others == {color: 'yellow', disposition: 'sunny'};
Run Code Online (Sandbox Code Playgroud)