Can't reference immediately destroyed props in a class component, do I need to use `this.props` every time?

In a stateless functional component, we can use destructured props

as function arguments:

const Blah = ({ hello, world }) {
  return <div>{hello} {world}</div>
}

      

This gives a very clean code, and not only is it obvious which props are being passed, we don't need to use it this.props

everywhere.

When used this.props

all the time in a component class

can take up a lot of space throughout the component and is not that nice to use:

class Blah extends Component {
  render() {
    return (
      <div>{this.props.hello} {this.props.world}</div>
    )
  }
}

      

My questions are, what approach can we take for components class

so that we don't need to use it this.props

everywhere?

One solution I can think of would be to destruct a method props

in a method render

, but of course you would need to do this for every method in class

if you wanted to use a destructured name.

+3


source to share


1 answer


No, I'm afraid there is no way. A functional component is essentially a functionrender()



class component, "purity" is kind of the same (:
+1


source







All Articles