Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method parameter: destructuring + keeping original parameter (ReactJS component) [duplicate]

is there any way to achieve method parameter destructuring, but also be able to get method parameter.

In the context of a React application with stateless components, I'd like to be able to replace

const MyComponent = (props) => {
  const {prop1, prop2} = props;
  return (
    <div className={prop1 + '-' + prop2}>
      <Child {...props}/>
    </div>
  ) 
}

with a more concise syntax like

const MyComponent = (props: {prop1, prop2}) (
  <div className={prop1 + '-' + prop2}>
    <Child {...props}/>
  </div>
) 

Is there any syntax like that available?

like image 839
Sebastien Lorber Avatar asked Nov 24 '16 16:11

Sebastien Lorber


2 Answers

we have this:

const MyComponent = ({ prop1, prop2, ...rest }) (
  <div className={prop1 + '-' + prop2}>
    <Child prop1={prop1} prop2={prop2} {...rest} />
  </div>
) 
like image 176
yadhu Avatar answered Oct 19 '22 19:10

yadhu


If you define your component as function, you can use arguments object:

function MyComponent({ prop1, prop2 }) (
  <div className={prop1 + '-' + prop2}>
    <Child {...arguments[0]}/>
  </div>
)
like image 2
madox2 Avatar answered Oct 19 '22 21:10

madox2