反应组件道具。如何获得内部对象?

时间:2018-09-04 11:10:02

标签: json reactjs

我有一个这样的类对象:

classes: {
    divBlock: "divBlockClass",
    rootBlock: "rootBlockClass"
}

如何获取divBlock属性并将其传递给otherProps剩余的类对象

const Component = ({height, className, classes /*i need to take a divBlock property*/, ...otherProps}) => {
    console.log(otherProps); //log props who are not pulled out
    return (
        <div>

        </div>
    )
};

2 个答案:

答案 0 :(得分:1)

您可以在函数主体中传播props

const Component = (props) => {
  const {height, className, classes, ...restProps} = props;
  const { divBlock, ...restClassProps } = classes;
  const otherProps = {...restProps, classes: restClassProps };
  console.log(otherProps); //log props who are not pulled out
  return <div />
};

答案 1 :(得分:0)

这样做:

const Component = ({
    height,
    className,
    classes : { divBlock }, ...otherProps
}) => {
    console.log(otherProps);

    return (
        <div>

        </div>
    )
};