How to return a function within a function and display it in return

user11395273

This is my state

    constructor(props) {
    super(props);
    this.state = {
        fToC: '',

And this is my function

    handleChange = async (value) => {
    this.setState({ value });
    await Axios.get(`http://dataservice.accuweather.com/`).then((res) => {
        this.setState({ weather: res.data });

        console.log('weather_Temperature:' + this.state.weather.DailyForecasts[0].Temperature.Maximum.Value);

  >>>>>> function FToC(fahrenheit) { <<<<<<<<<<<<<<< 
            let fTemp = fahrenheit;
            let fToCel = (fTemp - 32) * 5 / 9;
            let message = fTemp + '\xB0F is ' + fToCel + '\xB0C.';
            console.log('message' + message);
        } FToC(this.state.weather.DailyForecasts[0].Temperature.Maximum.Value);
        this.setState({ FToC }) 

And here I want to return the result of the function

        <p class="card-text">{this.state.FToC}</p> 

The Error:

Warning: Functions are not valid as a React child. This may happen if you return a Component instead of from render. Or maybe you meant to call this function rather than return it.

Roman Unt

So problem is you are trying to render a function which was set to the state

You need to return a value from your FToC function and then invoke it in setState method

setState({fToC: FToC(this.state.weather.DailyForecasts[0].Temperature.Maximum.Value))

Btw, this.setState({ FToC }) creates another field (FToC) in your state, you already have fToC

Code below should works well

handleChange = async (value) => {
    this.setState({ value });
    await Axios.get(`http://dataservice.accuweather.com/`).then((res) => {
        this.setState({ weather: res.data });

        console.log('weather_Temperature:' + this.state.weather.DailyForecasts[0].Temperature.Maximum.Value);

  function FToC(fahrenheit) {
            let fTemp = fahrenheit;
            let fToCel = (fTemp - 32) * 5 / 9;
            let message = fTemp + '\xB0F is ' + fToCel + '\xB0C.';
            return message;
        } ;
        this.setState({ fToC: FToC(this.state.weather.DailyForecasts[0].Temperature.Maximum.Value) }) ```

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to create functions within a function and return them?

How to return a value within a function with an event listener?

How to return an input within a def function with python

How to return the value given by the gps within a function

How to use return function to display any element

how return a variable from a function and display it

How do I return a function within another function

How to return the value of function 2 from within function 1

How to correctly return and display string value if function does not return true?

Return Observable within Async Function

Map is not a function within React return

basic function within object return

Is `return` within function a compulsory requirement?

Return values when recalling a function within a function

Javascript OOP - Function within return function

Python: Return the name of a function within a function within a function

Display Return from Firebase Function

Incorrect display of Chr() function return

How do I pass a return value to another function and assign the return value to a variable within that function?

How to get value return from a function within a loop?

How can I return a value within a nested function?

RxJava How to return more than 2 ArrayLists within Observable in a function

How to return json from callback function within the Lambda?

How to get the return type of a member function from within a class?

Swift: How do I return a value within an asynchronous urlsession function?

How to modify a global variable within a function and return a boolean in bash?

How can I return all functions defined within a function?

swift - how to return from a within a completion handler closure of a system function?

Python: How do I return to a variable within a function?