react native වල props හා state වලින් වෙන එක example එකක් එක්ක කියල දෙනවද දන්න කෙනෙක් ..තේරෙන්නෙම නෑ බං 


render(){
return (
<div>
<Child sirName="Snow" />
<div>
)
}
constructor(){
this.state = {
firstName : Jon
}
}
render(){
return (
<h1>First Name : {this.state.firstName} <br />
Second Name : { this.props.sirName} <h1>
)
}
[SIZE="5"]
First Name : Jon
Second Name : Snow
[/SIZE]
props contains information set by the parent component (although defaults can be set) and should not be changed.
state contains “private” information for the component to initialise, change, and use on it’s own.
props are a way of passing data from parent to child. ... State is reserved only for interactivity, that is, data that changes over time.
Props
class Hello extends React.Component<Props, any> {
render() {
return <h1>Hello from {this.props.name}!</h1>;
}
}
// invoke the component by passing props
ReactDOM.render(
<Hello name="Joseph"> />,
document.getElementById("main")
);
------------------------------------------------------------------
State
class Counter extends Component{
constructor(props){
super(props);
this.state = {counter: 0}
this.increment = this.increment.bind(this);
}
increment(){
this.setState({counter: this.state.counter + 1})
}
render(){
return(
<button onClick={this.increment}>Like</button>
<p>{this.state.counter}</p>
)
}

Saralawa props kiyanne component ekakata eyage calling component eken labena dewal.
state kiyanne component eka sathuwa danata tiyena dewal.
apita props wenas karanna baa namuth state eke tiyena ewa wenas karanna puluwan.
Example ekak kiwwoth
Parent.js
Code:render(){ return ( <div> <Child sirName="Snow" /> <div> ) }
Child.js
Code:constructor(){ this.state = { firstName : Jon } } render(){ return ( <h1>First Name : {this.state.firstName} <br /> Second Name : { this.props.sirName} <h1> ) }
Uda tiyena code eke Parent component eken Child component ekata call karanawa. Ethakota Parent component eken yawanawa data widiyata sirName kiyala attribute ekak Child component ekata. Eee wage ona ekak yawanna puluwan. Function ekak unath reference ekak widiyata eelaga component ekakakta yawanna puluwan.Code:[SIZE="5"] First Name : Jon Second Name : Snow [/SIZE]
Dan child component eka create wenakotama eyage props hatiyata ara sirName kiyana eka eyata tiyenawa. Thawa eyage state eke data widiyata FirstName kiyana eka tiyenawa. Mewa component eka athule ona thanakadi apita this.state hari this.props hari widiyata ganna puluwan. this.setState walin apita firstName kiyana eka wenas karannath puluwan.