Answer the question
In order to leave comments, you need to log in
Why is the component not updating after accepting props from redux?
Hello, there was such a problem
Functional component:
import React, {useEffect, useState} from 'react';
import {connect} from 'react-redux';
import {getWeather} from '../actions/getWeather.js';
const Weather = (props) =>{
const [isFetching, setFetching] = useState(true);
useEffect(()=>{
let lat;
let long;
navigator.geolocation.getCurrentPosition(position =>{
lat = position.coords.latitude+17;
long = position.coords.longitude;
props.onGetWeather(lat,long)
});
setFetching(props.weather.isFetching)
},[])
if(isFetching){
return(
<div id='loading'>
<h1>Loading... {props.weather.error}</h1>
</div>
)
}else{
return(
<React.Fragment>
<React.Fragment>
<h1>{props.weather.data.timezone}</h1>
<div className='weatherWrap'>
<h3>{props.weather.data.currently.temperature}</h3>
<h5>{props.weather.data.currently.summary}</h5>
</div>
</React.Fragment>
</React.Fragment>
)
}
}
export default connect(
state =>({
weather: state.weather,
}),
dispatch =>({
onGetWeather: (lat, long) =>{
dispatch(getWeather(lat, long))
}
})
)(Weather)
{
isFetching: false,
data: (данные с сервера)
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question