【发布时间】:2021-12-17 16:09:12
【问题描述】:
我是 React、Redux 和 hooks 的新手,我正在构建一个简单的应用程序。第一个组件是一个搜索栏,你输入一个 pokemon 名称,它会调用一个 API (pokeapi)(我也使用 react-thunk)并返回一个包含该 pokemon 信息的新组件。
我正在构建结果页面组件,我可以控制台记录状态并查看整个对象,但我无法操作对象内部的任何内容。
例如:console.log(pokemonState) 返回整个对象及其嵌套属性 // console.log(pokemonState.name) 返回未定义。
这是我的代码:App.js
import { Route, NavLink, Redirect } from 'react-router-dom';
import PokemonSearch from './container/PokemonSearch';
import PokemonResult from './container/PokemonResult';
function App() {
return (
<div className="App">
<nav>
<NavLink to={'/'}>Search</NavLink>
</nav>
<h1>TEST</h1>
<Route path={'/'} exact component={PokemonSearch} />
<Route path={'/pokemon/:pokemon'} exact component={PokemonResult} />
<Redirect to={'/'} />
</div>
);
}
export default App;
上面我没有贴,但是顶层的index.js也被Provider包裹了。 PokemonSearch.js
import React, { useState } from 'react';
import { getPokemonData } from '../actions/pokemonAction';
import { useDispatch } from 'react-redux';
import '../styles/PokemonSearch.css';
const PokemonSearch = (props) => {
const [search, setSearch] = useState('');
const dispatch = useDispatch();
const FetchData = () => {
dispatch(getPokemonData(search));
};
const handleChange = (e) => {
setSearch(e.target.value);
};
const handleSubmit = (e) => {
e.preventDefault();
FetchData();
props.history.push(`/pokemon/${search}`);
};
return (
<div>
<div className="bar">
<form onSubmit={handleSubmit}>
<input
type="text"
className="searchInput"
placeholder="Search a Pokemon"
onChange={handleChange}
/>
</form>
</div>
<div></div>
</div>
);
};
export default PokemonSearch;
动作
import axios from 'axios';
export const getPokemonData = (pokemon) => async (dispatch) => {
try {
dispatch({
type: 'POKEMON_DATA_LOADING',
});
const res = await axios.get(`https://pokeapi.co/api/v2/pokemon/${pokemon}`);
dispatch({
type: 'POKEMON_DATA_SUCCESS',
payload: res.data,
pokemonName: pokemon,
});
} catch (e) {
dispatch({
type: 'POKEMON_DATA_FAIL',
});
}
};
减速器
const DefaultState = {
loading: false,
data: [],
errorMsg: '',
};
const PokemonSearchReducer = (state = DefaultState, action) => {
switch (action.type) {
case 'POKEMON_DATA_LOADING':
return {
...state,
loading: true,
errorMsg: '',
};
case 'POKEMON_DATA_FAIL':
return {
...state,
loading: false,
errorMsg: "Error: cannot find the pokemon you're looking for.",
};
case 'POKEMON_DATA_SUCCESS':
return {
...state,
loading: false,
errorMsg: '',
data: {
...state.data,
data: action.payload,
},
};
default:
return state;
}
};
export default PokemonSearchReducer;
Root Reducer 和存储
import { combineReducers } from 'redux';
import PokemonSearchReducer from './PokemonSearchReducer';
const rootReducer = combineReducers({
PokemonSearch: PokemonSearchReducer,
});
export default rootReducer;
import { createStore } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import { applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers/rootReducer';
const Store = createStore(
rootReducer,
composeWithDevTools(applyMiddleware(thunk))
);
export default Store;
PokemonResult.js
import { useSelector } from 'react-redux';
const PokemonResult = (props) => {
const pokemonState = useSelector((state) => state.PokemonSearch);
console.log(pokemonState.name);
return (
<div>
<h1>Title</h1>
</div>
);
};
export default PokemonResult;
console.log(PokemonState) 输出: what apis return when you type a valid pokemon name
所以我想知道出了什么问题,为什么我可以 console.log 整个状态而不是我的组件 (PokemonResult) 中的特定属性?
感谢您的帮助。
【问题讨论】:
-
你能把console.log(pokemonState)的输出放上来吗?
-
嗨 Nicolas,这是输出的屏幕截图:screenshot link
-
请修剪您的代码,以便更容易找到您的问题。请按照以下指南创建minimal reproducible example。
标签: reactjs redux react-redux react-hooks redux-thunk