4

I have a simple react component. I would like to assign a variable inside the component method to props. I've tried this method works with functional components:

class Pets extends React.component {
  constructor(props) {
    super(props)
  }

 const {dog, cat, frog} = props
 // the code on the line above does not work

 render() {
   return (
     <div>
       {dog.name}
       {cat.name}
     </div>
   )
 }
}
2
  • does it throw an error or does it just not assign? are you passing in the props to the component? Commented Sep 14, 2018 at 15:52
  • What were you expecting? It's just bad class declaration - 'Unexpected token. A constructor, method, accessor, or property was expected.' error appears in stackblitz. Commented Sep 14, 2018 at 16:35

1 Answer 1

11

That will not work. props is a property on the instance of the component you created with <Pets /> JSX for example. It needs to be inside the instance methods of the class Pets. It will work if you do it in the render method by const {dog, cat, frog} = this.props. props in the class body as you have now in the question is not what them expected to be.

Inside the constructor function just const {dog, cat, frog} = props will work, because here props object is received as an argument. so this.props (after the super(props) line)andprops` is same object.

Generally you unpack props where you need them, for example you need those inside the render to create some output, so you unpacked it there. I tried to explain in this answer what props means in the class body, and instance methods body, how you can access the props in different methods etc. But as xadm said, unpacking them inside the constructor by thinking you will access them later in other methods will not work due to the scope boundary, unless you store them as a property to the instance again using this.dog = props.dog. But this is an horrible idea, never do this. Just unpack them when you need any property from props object in that place only.

Sign up to request clarification or add additional context in comments.

9 Comments

in constructor it will work but vars declared(f.e. dog ) won't be accessible in render because of scope.
@xadm yes,, I said it needs this.props.
I mean - const declared this way in constructor (OP intendet to be class scope declaration?) won't be available in render.
sorry, @xadm, what do you mean it won't be available in the render? the solution works for me
const {dog, cat, frog} = props in constructor gives dog valid only in constructor scope. Refering to dog in render (dog.name) won't give you access the same const (dog declared in constructor). You have to use another const {dog, cat, frog} = this.props. Of course both are derived from the same props object but I've read this code in an intention to be object scope declaration like const this.dog = props.dog from constructor will be accessible as this.dog (this.dog.name) in render.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.