React Transferring Props except one

前端 未结 5 1971
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-23 13:05

React suggests to Transfer Props. Neat!

How can I transfert all but one?

render: function(){
  return (

        
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-23 13:49

    If you have a lot of props you don't want in ...rest e.g. defaultProps, it can be annoying to write all of them twice. Instead you can create it yourself with a simple loop over the current props like that:

    let rest = {};
    Object.keys(this.props).forEach((key, index) => {
        if(!(key in MyComponent.defaultProps))
           rest[key] = this.props[key];
    });
    

提交回复
热议问题