Why the function argument returns undefined when using props?

random_user

I am building an app using react-native. I have 3 components namely Ind.js, Buttons.js, Rain.js. I am required to select an option in Rain and save it in the state of Ind for further processing. Since Rain component is not directly related to Ind but is connected via a navigation route in Buttons. I am using react-navigation.

To do so I created a function onSelect() in Ind which does the setState and passed it to Buttons via props and passed again to Rain then I executed the function, the problem is the function is getting called but no parameters are passed ie, it console.logs null then undefined.

I have tried to console.log the parameters that are passed to Ind.

Ind.js

export default class Ind extends Component {
constructor(){
super();
this.state = { report: null}
}
onSelect = (newreport) => {
      this.setState({
        report: newreport
      })
      console.log("Parameter: ", newreport)
      console.log("State: ", this.state.report)
    }

render(){
return(
<Buttons selectReport={() => this.onSelect()}
)

}

}

Buttons.js

export default class Buttons extends Component{
constructor(props){
super(props);
}
render(){
return(
<TouchableOpacity
    onPress={() => {this.props.navigation.navigate('Rain',{
                  selectReport: this.props.selectReport });
                }}>
          <Text style={styles.text}>Rain</Text>
 </TouchableOpacity>
)
}
}

Rain.js

export default class Rain extends Component{
constructor(props){
super(props);
this.state = {
selection: "Test"
}
this.selectOption = this.selectOption.bind(this);
}
selectOption = () => {
this.props.navigation.state.params.selectReport(this.state.selection)
}
}

The console log return first Parameter: undefined State: null which is understandable because nothing is clicked but after clicking it shows Parameter: undefined State: undefined. What is happening? I am a beginner and is there something wrong in binding or sending the props? Please Explain.

ravibagul91

When working with arrow function, you need to call like,

<Buttons selectReport={() => this.onSelect} > //without parenthesis

also setState is async so you need to use callback in setState to print value.

You need to do this,

export default class Ind extends Component {
  constructor(){
   super();
   this.state = { report: null}
  }
  onSelect = (newreport) => {
      console.log("Parameter: ", newreport)
      this.setState({
        report: newreport
      },()=> console.log("State: ", this.state.report)) //prints in callback
  }

  render(){
   return(
    <Buttons selectReport={() => this.onSelect}>
   )
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

why function returns undefined when it should be 'n/a'?

Why are my props `undefined` when using redux and react.js?

Why Could Not Deduce Template Argument When Function Returns a Struct Template

Why is "this" in an anonymous function undefined when using strict?

Why my async function returns an undefined when i try to access it?

When nesting components this.props.parent returns "undefined". How do I access a parents function?

Javascript function returns undefined, why?

Testing Vuex getters with argument returns undefined is not a function

JavaScript typeof function passed as an argument returns undefined

Passing argument to async / await function returns "undefined"

Why props is undefined inside addEventListener function in React

Arrow function is undefined when passed as props

React props function is undefined when testing

React - undefined props error when rendering function

Using the length function returns undefined

Why is the variable argument of this required function returning undefined?

sending a parameter to a function returns Cannot read property 'props' of undefined

Why TypeScript compiler does not give error when function returns undefined while function's return type is number?

Why do I get undefined when using this function?

Why is the undefined function levity-polymorphic when using it with unboxed types?

"this" is undefined when function is passed as argument in chain

Function argument is 'Null or Undefined' when it is supplied

Why does this function returns the last argument when called right after definition

JavaScript Function call returns undefined, why?

Why this recursive javascript function returns undefined?

Why console.log function returns undefined?

Why ? Object params of function returns undefined JS

Why my debounce function returns undefined value?

Is using a member function as an argument to a constructor undefined behavior?

TOP Ranking

HotTag

Archive