I need to fully grasp the idea behind State and Props. Anyone willing to help demystify these terms in the simplest way.
For further actions, you may consider blocking this person and/or reporting abuse
I need to fully grasp the idea behind State and Props. Anyone willing to help demystify these terms in the simplest way.
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (10)
I'd really welcome feedback if this isn't right or the best way of explaining them, but here's a shot:
State holds data and variables which are specific to a component. They work like object/class properties.
Props are options which you can pass into a component when you create them, often as an attribute, such as
<Person eyes="brown">- the prop is eyes, value is brown. You have to handle that in your component.What is the difference between 'state' and an OOP object?
Thanks for breaking this down
Maybe not quite accurate, but here I go:
Think in a sandwich. You can imaging two types of ingredients, there are the ones that you put in like tomato, bacon or cheese. This come from the outside world. But the bread itself is also made of some ingredients, those are already baked in. You could make a sandwich component like this:
See. In here you pass a vegetable of your choice to the sandwich component. You can think of state as values that live inside the component. And props as values that come from the outside.
I also like to think of components as functions (in some libraries that is actually the case), and in that sense the "props" are just the arguments that you give to the component and state are the variables that you declare inside the component.
Hi Gift. I'm glad to be able to help out.
State is Just Data. You know.. The usual data we see, share, use everyday.
In programming, we just kick around 2 things all our lives:
Data & Algorithms (you know..to manipulate data)
So just in case you're building a web based application. State is basically the data associated, down from usernames and JWT's to
button-hovercss classes.Props is just more dynamic data ...this is more simpler than state to grasp, imagine you need to pass data(state) around from one house (function) to another to make use of it.
State isn't just associated with web components.
Games => Scores, health, Level, Position on screen of player etc.
Programming Languages => Function names, Object instances etc.
Human behavior => Emotions, Age, Portfolio etc.
Anything that holds information that can be changed/or not as well.
Props are a bunch of things someone else gives you. Someone else is responsible for storing them and handling changes to them (like giving you a function to call when you want something to change).
State is a bunch of things you store yourself that noone gives you and can pass along to others. You are responsible for storing it and deciding what to do when someone wants to change it. You can pass functions that can be called by other components to tell you when they want something to change.
It's up to you to decide if a given piece of data should live outside of your component, or inside the state of some component.
Generally, it's best to avoid all state in a component as it's not accessible to the outside world, and you definitely do not want to store values that are supposed to be coming from props in the state. That will hurt you later, since you would have to guarantee that your internal state always stays in sync with the props coming in.
I know this may be a bit premature, so feel free to skip the rest of this comment, but it will make so much sense later when you're comfortable with the concepts.
There are techniques to avoid having state within your component, like using a state library (tiny ones like Unistore, Reduxless, etc.) or just a simple component you can build using
withState:This is how you use it:
When used in your app:
I also advocate an approach where all of the logic about converting data, formatting it, etc. is moved outside of the component, leaving just a small template behind. You can read about it in my other post:
Make testable components using the Humble Object pattern
Klemen Slavič
Imagin state as a JavaScript object.
State comes from inside the parent or inside the children, and can be traspased to another children as a prop. You can only change the state inside the creator of the state.
Props comes from the parent to the children. The children can’t change the prop (it’s read only).
Very helpful, TY. Coming from a backend programming perspective and just recently learning VueJS state confused me a bit. Your explanation helps.