简体   繁体   English

反应传播运算符 (...rest) 替代方案?

[英]React spread operator (…rest) alternatives?

I'm not able to include babel-preset-stage-3 in my pipeline.我无法在我的管道中包含babel-preset-stage-3 Are there any alternatives to the spread operator?点差运算符有什么替代方法吗?

I'm trying to compile the following code and it is giving syntax errors:我正在尝试编译以下代码,但出现语法错误:

const PrivateRoute = ({ component: Component, ...rest }) => (
  <Route
    {...rest}
    render={props =>
      fakeAuth.isAuthenticated ? (
        <Component {...props} />
      ) : (
        <Redirect
          to={{
            pathname: "/login",
            state: { from: props.location }
          }}
        />
      )
    }
  />
);

Using lodash.omit :使用lodash.omit

const PrivateRoute = (props) => {
  const Component = props.component;
  const rest = omit(props, ['component'])
  return (
  <Route
    {...rest}
    render={props =>
      fakeAuth.isAuthenticated ? (
        <Component {...props} />
      ) : (
        <Redirect
          to={{
            pathname: "/login",
            state: { from: props.location }
          }}
        />
      )
    }
  />
);
}

Install babel-plugin-transform-object-rest-spread安装babel-plugin-transform-object-rest-spread

npm install --save-dev babel-plugin-transform-object-rest-spread

In your .babelrc file, add the following lines在您的 .babelrc 文件中,添加以下几行

{
  "plugins": ["transform-object-rest-spread"]
}

Try using this function for simplicity:为简单起见,尝试使用此函数:

                    var vals;
        console.log(typeof out)
        isobj = typeof(out) === 'object'?true:false


                if (Object.values) {
                    vals = Object.keys(obj).map(function(i) { 
           if(isobj)
             out[i] = obj[i]; 

                    })
                }else
                    vals= Object.values(obj) 
                return vals;
````}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM