Difference between react props vs. state

One of the core concepts of react is the difference between props and state. Only changes in props and state trigger react to re-render components and update the DOM.

The biggest difference is that re-rendering of the component based on input in state…


This content originally appeared on DEV Community and was authored by Hans

One of the core concepts of react is the difference between props and state. Only changes in props and state trigger react to re-render components and update the DOM.

The biggest difference is that re-rendering of the component based on input in state is done entirely within the component, whereas you with using props can receive new data from outside the component and re-render.

Props

props allows you to pass data from a parent component to a child component.

//Parent Component
const books = () => {
    return (
<div>
 <Book title = "Data structures and algorithms with JAVA" />
 </div>
   );
}
//Child component
const book = (props) => {
    return ( 
        <div>
            <h1>{props.title}</h1>
        </div>
    )
}

Enter fullscreen mode Exit fullscreen mode

Explanation: Now. ‘props’ is passed in to the child component and the functional component the passes the ‘props’ as an argument which in turn will be handled as an object. The property ‘title’ is accessible in the child component from the parent component.

State

Only class-based react components can define and use state. It’s although possible to pass state to a functional component but functional components can’t edit them directly.

class NewBook extends Component {
    state = {
        number: ''
    };
    render() {
        return ( 
            <div>{this.state.number}</div>
        );
    }
}
Enter fullscreen mode Exit fullscreen mode

As you can see the NewBook component contains a defined state. This state is accessible through this.state.number and can be returned in the render() method.


This content originally appeared on DEV Community and was authored by Hans


Print Share Comment Cite Upload Translate Updates
APA

Hans | Sciencx (2021-02-28T08:45:23+00:00) Difference between react props vs. state. Retrieved from https://www.scien.cx/2021/02/28/difference-between-react-props-vs-state/

MLA
" » Difference between react props vs. state." Hans | Sciencx - Sunday February 28, 2021, https://www.scien.cx/2021/02/28/difference-between-react-props-vs-state/
HARVARD
Hans | Sciencx Sunday February 28, 2021 » Difference between react props vs. state., viewed ,<https://www.scien.cx/2021/02/28/difference-between-react-props-vs-state/>
VANCOUVER
Hans | Sciencx - » Difference between react props vs. state. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/28/difference-between-react-props-vs-state/
CHICAGO
" » Difference between react props vs. state." Hans | Sciencx - Accessed . https://www.scien.cx/2021/02/28/difference-between-react-props-vs-state/
IEEE
" » Difference between react props vs. state." Hans | Sciencx [Online]. Available: https://www.scien.cx/2021/02/28/difference-between-react-props-vs-state/. [Accessed: ]
rf:citation
» Difference between react props vs. state | Hans | Sciencx | https://www.scien.cx/2021/02/28/difference-between-react-props-vs-state/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.