javascript - Accessing this.state value to other Class [React Native] -
i wanna ask how can access this.state.samplestring other class.. here's code
class mainclass extends component { constructor(props){ super(props) this.state = { samplestring: 'test string' } this.getvalue = this.getvalue.bind(this); } getvalue(){ //console.log(this.state.samplestring); return this.state.samplestring } }
=========
this function second class value of "this.state. samplestring" mainclass
function getvaluefrommainclass() { var stringfromclassheader = () => {headerwithbg.getvalue()} console.log(stringfromclassheader.samplestring); }
why returns "undefined"?
thanks alot. im new in react native.
you can send this.state.samplestring
prop other components , use there. simple example of below:
class mainclass extends component { constructor(props){ super(props) this.state = { samplestring: 'test string' } this.getvalue = this.getvalue.bind(this); } getvalue(){ //console.log(this.state.samplestring); return this.state.samplestring } render(){ return ( <childclass samplestring={this.state.samplestring}/> ) } } class childclass extends component { somefunction() { //console.log(this.props.samplestring); return this.props.samplestring } render(){ return ... } }
Comments
Post a Comment