What is the difference between State and props in React Native

State

This is a data stored in a specific component. The component is responsible to update the state using the setState function means the state is changed from inside that component in which the state is declared. The state is not accessed or updated from outside the component.

A simple example of state in React Native.

class StateExample extends React.component{
     constructor(props) {
    super(props);

    this.state = {
          exampleState : 'this is the state'
     }


     render(){
	     return(
	     	<Text>{this.state.exampleState}</Text>
	     )
     }
}

Props

Props are the data that are passed from child component.
Props are read only in the child component.Only the parent component can update it.

A simple example of props in React Native.

class StateExample extends React.component{
 render(){
     return(
	     	<Text>{this.props.exampleProps}</Text>
	   )
}
// passing the props
<StateExample exampleProps='this data is passed to child' />
<StateExample exampleProps='this is also passed to child' />

The props passed from the parent to child component is accessed using this.props.

Renish

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top