2

For eample:

<Counter start="10">

... 

export default class Counter extends React.Component {

    constructor(props) {

        super();

        this.state = {
            start: props.start
        };
    }
}

I googled this question and I got an idea that the answers I found are outdated

The questions on StackOVerflow

But I found this post in React blog: React v0.13.0 Beta 1

And in that post author does exactly what I want, as I understand getDefaultProps is deprecated now.

So the question is: Is passing state through props still an anti-pattern?

Community
  • 1
  • 1
SmxCde
  • 5,053
  • 7
  • 25
  • 45
  • Yes it is. Post why you need to do this and we can find an alternative. – Martin Dawson Dec 30 '16 at 11:29
  • The example is in the question: I have a counter and I need to pass an initial state to the component. How do I do that if not through properties... – SmxCde Dec 30 '16 at 21:07

1 Answers1

0

IMHO 'yes' because you give the impression that changing the prop value would change the behaviour of the component which won't happen. Your component would behave exactly the same when I change the start param.

Impressions are cool but needs are real. At times when I need this type of behaviour I simply name my prop accordingly like initialFoo or defaultBar.

  • But there can be scenarios where changing the state variable (which got its initial value through the props) changes the behavior of the component. In those cases wouldn't this be the way to go – Swapnil Dec 30 '16 at 10:55
  • Yes, that was what I meant when I said 'needs are real'; sorry if that was brief. The need is there; for example when you want to implement some behaviour which resembles the behaviour of uncontrolled components where you provide only the defaultValue via props. I recommend having a naming convention for these type of props though... – R. Haluk Öngör Dec 30 '16 at 11:00
  • Oh. I completely misunderstood. :p – Swapnil Dec 30 '16 at 11:02
  • Sorry for the confusing way I asked, is "Yes" an answer to question "Can I do" or "Is it still anti-pattern"? – SmxCde Dec 30 '16 at 21:06
  • I should have been more verbose; sorry about that. After giving some more thought about the issue; and this is only my humble opinion, I would say not an anti-pattern but a bad practice. It is not an anti-pattern because the pattern is used by uncontrolled components and there is no way around it. It is a bad practice *unless you define a clear naming convention* because of the reasons I've stated before. – R. Haluk Öngör Jan 02 '17 at 08:06