Redux 程序 - 以下代码的输出不会在 VS Code 终端中打印。没有错误,但未打印输出
Redux program - Output is not printed in VS Code terminal for below code. There are no errors but output is not printed
const FETCH_USERS_REQUEST = 'FETCH_USERS_REQUEST'
const FETCH_USERS_SUCCESS = 'FETCH_USERS_SUCCESS'
const FETCH_USERS_FAILURE = 'FETCH_USERS_FAILURE'
下面的代码没有显示错误,但它不起作用。我在这里没有看到任何错误,因为我是 Redux 的新手。所以我请求任何人调查这个并找出为什么输出没有打印到控制台。我可能在这里缺少一些非常基本的东西。请check.please检查图像以获取剩余代码。
//Reducer function
const reducer = (state=initialState,action)=>{
switch(action.type)
{
case 'FETCH_USERS_REQUEST':
return {
...state,
loading:true
}
case 'FETCH_USERS_SUCCESS':
return {
...state,
loading:false,
users: action.payload,
error:''
}
case 'FETCH_USERS_FAILURE':
return {
...state,
loading:false,
users: [],
error:action.payload
}
}
}
//common channel
const fetchUsers = () => {
return function(dispatch){
dispatch(fetchUsersRequest())
axios.get('https://jsonplaceholder.typicode.com/users')
.then(response => {
const users = response.data.map(user => user.id)
dispatch(fetchUsersSuccess(users))
})
.catch(error => {
dispatch(fetchUsersFailure(error.message))
})
}
}
const store = createStore(reducer, applyMiddleware(thunkMiddleware))
console.log("Debugging")
store.subscribe(() => {console.log((store.getState()))})
console.log("post subscribe")
store.dispatch(fetchUsers)
问题出在这一行:
store.dispatch(fetchUsers)
您必须实际调用这样的函数才能使其工作:
store.dispatch(fetchUsers())
const FETCH_USERS_REQUEST = 'FETCH_USERS_REQUEST'
const FETCH_USERS_SUCCESS = 'FETCH_USERS_SUCCESS'
const FETCH_USERS_FAILURE = 'FETCH_USERS_FAILURE'
下面的代码没有显示错误,但它不起作用。我在这里没有看到任何错误,因为我是 Redux 的新手。所以我请求任何人调查这个并找出为什么输出没有打印到控制台。我可能在这里缺少一些非常基本的东西。请check.please检查图像以获取剩余代码。
//Reducer function
const reducer = (state=initialState,action)=>{
switch(action.type)
{
case 'FETCH_USERS_REQUEST':
return {
...state,
loading:true
}
case 'FETCH_USERS_SUCCESS':
return {
...state,
loading:false,
users: action.payload,
error:''
}
case 'FETCH_USERS_FAILURE':
return {
...state,
loading:false,
users: [],
error:action.payload
}
}
}
//common channel
const fetchUsers = () => {
return function(dispatch){
dispatch(fetchUsersRequest())
axios.get('https://jsonplaceholder.typicode.com/users')
.then(response => {
const users = response.data.map(user => user.id)
dispatch(fetchUsersSuccess(users))
})
.catch(error => {
dispatch(fetchUsersFailure(error.message))
})
}
}
const store = createStore(reducer, applyMiddleware(thunkMiddleware))
console.log("Debugging")
store.subscribe(() => {console.log((store.getState()))})
console.log("post subscribe")
store.dispatch(fetchUsers)
问题出在这一行:
store.dispatch(fetchUsers)
您必须实际调用这样的函数才能使其工作:
store.dispatch(fetchUsers())