javascript - What is difference between render function with destructing syntax and with plain empty parameter? -
i coded react , seemed verbose use this.props, googled articles , figured out how , try code it.
class mycomponent extends component { // usual way render() { return <div>{this.props.value}, {this.props.value2}</div> } } class mycomponent extends component { // way of avoding this.props render({value, value2}){ return <div>{value}, {value2}</div>; } } class parentcomponent extends component { render(){ return <mycomponent value={1} value2={2} /> } } i not have sense of how handled internally, tried transpile @ https://babeljs.io/repl still difficult understand advantages , disadvantages these syntaxes have.
here summary of questions:
- is idea use destructuring reduce code?
- if not, wonder why not use it.
there no benefits other making references said variables shorter. in other words, less typing , better readability.
for more info, have @ this page on mdn:
the destructuring assignment syntax javascript expression makes possible unpack values arrays, or properties objects, distinct variables.
here few other examples, found more common:
desctructuring in stateless functional components:
const mycomponent = ({value, value2}) => ( <div>{value}, {value2}</div> ); destructuring in reactcomponent class:
class mycomponent extends component { render(){ let {value, value2} = this.props; return <div>{value}, {value2}</div>; } } for stateful components, can same state variables.
Comments
Post a Comment