1

I understand that there's two ways to pass components data: props and state. But why would one need a prop over a state? It seems like the state object could just be used inside the component, so why pass the prop parameters in markup?

Shai UI
  • 50,568
  • 73
  • 204
  • 309
  • 2
    `prop` is the object passed by the parent component to child component and `state` is local to a particular component. – Prakash Sharma Sep 19 '17 at 18:27
  • 2
    Possible duplicate of [What is the difference between state and props in React?](https://stackoverflow.com/questions/27991366/what-is-the-difference-between-state-and-props-in-react) – teju c Sep 19 '17 at 18:29
  • "I understand that there's two ways to pass components data: props and state" No, there's only one way to pass data: props. state holds data. it's the state of the component. to pass this state data to another component you need props – Cruiser Sep 19 '17 at 18:30
  • you don't actually pass state, you pass a property from a state via a prop of the child component. – bennygenel Sep 19 '17 at 18:30

2 Answers2

0

Props are set externally by a parent component. E.g.;

render() {
    return <ChildComponent someProp={someValue}/>;
}

State is set internally, and often triggered by an user event within a child. E.g.;

handleUserClickedButton: () {
    this.setState({
        buttonClicked: true
    });
},

render() {
    return <button onClick={this.handleUserClickedButton}/>;
}

So, props are a way for data to go from parent to child. State is a way for data to be managed within a singular component, and possibly have changes to that data triggered by children. In effect, they represent data traveling in 2 opposite directions, and the way in which they are passed is entirely unique.

Jake Haller-Roby
  • 6,335
  • 1
  • 18
  • 31
  • so you can't pass state between a parent to child? for example, can't the parent change the state and then the child gets the new state. – Shai UI Sep 19 '17 at 18:36
  • If the child "gets the new state", it does so in the form of receiving a prop; `` is a valid and typical piece of code that would take a value from state and pass it down to a child as a prop. – Jake Haller-Roby Sep 19 '17 at 18:38
  • why does the child not just have a shared state? for example, each component (or sub-component) could just do a "this.state" to get the current state of the program – Shai UI Sep 19 '17 at 18:39
  • Because each component has a unique state. State is not shared at all. – Jake Haller-Roby Sep 19 '17 at 18:39
  • the same way you can't access variables from other functions. _scope_, _context_ are the basics of javascript – Sagiv b.g Sep 19 '17 at 18:40
0

There are two ways to "pass" or access data from outside your component but state is not one of them.
The two ways are:
Props - which a parent component pass down to the child component.
Context - which you can "skip" the direct parent in the tree.

The state is an internal object which no other component has access to it unless you pass it explicitly (via the two ways mentioned above).

So basically your question is not accurate as you can't really compare the two.
I think what you are really asking is why using a state-less instead of a state-full component.
Which you can find an answer here in Stack-overflow or in other websites.

Edit
A followup to some of your comments.

why does the child not just have a shared state? for example, each component (or sub-component) could just do a "this.state" to get the current state of the program

  1. The same way you can't share or access private objects in other functions.
  2. This is by design, you share things explicitly and you will pass only what the component needs. For example, look it this page of stack-overflow, lets say the voting buttons are components, why would i pass them the whole state if it only needs the vote count and 2 onClick event listeners? Should i pass the current logged in user or maybe the entire answers rendered in this page?

so you can't pass state between a parent to child? for example, can't the parent change the state and then the child gets the new state

This is exactly what the props or context should do, provide an API for sharing data between parents and children though we keep it in a one way data flow, from parents to children, you can't pass props upwards. but you invoke handlers passed down to your child components and pass data through that handler.

Sagiv b.g
  • 30,379
  • 9
  • 68
  • 99