Я пытаюсь показать загрузку, пока все компоненты внутри RootContainer не завершат свои вызовы API, но мое приложение работает в бесконечном l oop вызываемого действия диспетчеризации, и приложение всегда остается в состоянии загрузки. Он совершил около 1300 звонков за 30 секунд. Вот мой код. Я не могу найти, почему приложение работает в infinte l oop.
App. js
import React, { Component } from 'react';
import RootContainer from './components/RootContainer'
import {Provider} from 'react-redux'
import store from './redux/store';
class App extends Component {
render() {
return (
<Provider store={store}>
<div className="App">
<RootContainer/>
</div>
</Provider>
)
}
}
export default App;
RootContainer. js
import React from 'react'
import {connect} from 'react-redux'
import {mapStateToProps , mapDispatchToprops} from './mapFunctions.js'
import UserContainer from './UserContainer'
import TodoContainer from './TodoContainer'
class RootContainer extends React.Component{
render(){
if(this.props.userData.count===0){
return (
<div>
<UserContainer/>
<TodoContainer/>
</div>
);
}else{
return(
<h2> Loading... </h2>
)
}
}
}
export default connect(mapStateToProps,mapDispatchToprops)(RootContainer)
UserContainer. js
import React from 'react'
import {connect} from 'react-redux'
import {mapStateToProps , mapDispatchToprops} from './mapFunctions.js'
class UserContainer extends React.Component{
constructor(props){
super(props)
this.state={
userList:[]
}
}
componentDidMount() {
this.props.fetchUsers('https://jsonplaceholder.typicode.com/users').then(data =>{
this.setState({
userList:data
})
})
}
render(){
console.log("count = "+this.props.userData.count);
return (
<div>
<h2>User List</h2>
<ul>
{this.state.userList.map(item => {
return <li>{item.name}</li>;
})}
</ul>
</div>
)
}
}
export default connect(mapStateToProps,mapDispatchToprops)(UserContainer)
TodoContainer. js
import {connect} from 'react-redux'
import { fetchUsers } from '../redux'
import {mapStateToProps , mapDispatchToprops} from './mapFunctions.js'
class TodoContainer extends React.Component{
constructor(props){
super(props)
this.state={
todoList:[]
}
}
componentDidMount() {
this.props.fetchUsers('https://jsonplaceholder.typicode.com/todos').then(data =>{
this.setState({
todoList:data
})
})
}
render(){
return (
<div>
<h2>ToDo List</h2>
<ul>
{this.state.todoList.map(item => {
return <li>{item.title}</li>;
})}
</ul>
</div>
)
}
}
export default connect(mapStateToProps,mapDispatchToprops)(TodoContainer)
UserActions. js
import { INCREMENT_COUNT ,DECREMENT_COUNT} from "./userTypes"
import axios from 'axios'
export const incrementCount = () =>{
return {
type:INCREMENT_COUNT
}
}
export const decrementCount = () =>{
return {
type:DECREMENT_COUNT
}
}
export const fetchUsers = (url) =>{
console.log("fetchUsers called" + url)
return(dispatch) => {
dispatch(incrementCount())
return fetch(url)
.then(response => {
dispatch(decrementCount());
return response.data
})
.catch(error => {
const errorMsg = error.message
console.log("error = "+errorMsg)
dispatch(decrementCount());
})
}
}
UserReducer. js
import {INCREMENT_COUNT ,DECREMENT_COUNT} from "./userTypes"
const initialState = {
count:0,
}
const reducer = (state = initialState,action) => {
switch(action.type){
case INCREMENT_COUNT:
console.log("reducer called =increment")
return{
count: state.count+1,
}
case DECREMENT_COUNT:
return{
count:Math.max(0,state.count-1),
}
default: return state
}
}
export default reducer
mapFunctions. js
import { fetchUsers } from '../redux'
export const mapStateToProps = state =>{
return {
userData: state.user
}
}
export const mapDispatchToprops = dispatch =>{
return {
fetchUsers: (url) => dispatch(fetchUsers(url))
}
}
rootReducer. js
import { combineReducers } from 'redux'
import userReducer from './user/userReducer'
const rootReducer = combineReducers ({
user:userReducer
})
export default rootReducer
store. js
import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk'
import { composeWithDevTools } from 'redux-devtools-extension'
import logger from 'redux-logger'
import rootReducer from './rootReducer'
const store = createStore(rootReducer,
composeWithDevTools(applyMiddleware(thunk,logger))
)
export default store