无法使用 combine reducer REACT 显示数据
Can't display data using combine reducer REACT
without redux it works so that not a api connection problem
我有一个连接到代理的快速应用程序我已经设法在 React 中显示我的数据,但现在我想在 redux 中制作它 soo:
这是我的问题,我已经制作了所有 reducers/action、store 和 combine reducer,但是 我没有在我的页面中看到任何数据 我没有任何错误
这是我的代码:
Action
export const api = ext => `http://localhost:8080/${ext}`;
//
// ─── ACTION TYPES ───────────────────────────────────────────────────────────────
//
export const GET_ADVERTS = "GET_ADVERTS";
export const GET_ADVERT = "GET_ADVERT";
//
// ─── ACTION CREATORS ────────────────────────────────────────────────────────────
//
export function getAdverts() {
return dispatch => {
fetch("adverts")
.then(res => res.json())
.then(payload => {
dispatch({ type: GET_ADVERTS, payload });
});
};
}
export function getAdvert(id) {
return dispatch => {
fetch(`adverts/${id}`)
.then(res => res.json())
.then(payload => {
dispatch({ type: GET_ADVERT, payload });
});
};
}
reducer
import { combineReducers } from "redux";
import { GET_ADVERTS, GET_ADVERT } from "../actions/actions";
const INITIAL_STATE = {
adverts: [],
advert: {}
};
function todos(state = INITIAL_STATE, action) {
switch (action.type) {
case GET_ADVERTS:
return { ...state, adverts: action.payload };
case GET_ADVERT:
return { advert: action.payload };
default:
return state;
}
}
const todoApp = combineReducers({
todos
});
export default todoApp;
index.js
//imports
const store = createStore(todoApp, applyMiddleware(thunk));
ReactDOM.render(
<Provider store={store}>
<BrowserRouter>
<App />
</BrowserRouter>
</Provider>,
document.getElementById("app")
);
My advertlist page :
//imports..
class Adverts extends Component {
componentDidMount() {
this.props.getAdverts();
}
render() {
const { adverts = [] } = this.props;
return (
<div>
<Header />
<h1>Adverts</h1>
{adverts.map(advert => (
<li key={advert._id}>
<a href={"adverts/" + advert._id}>
{advert.name} {advert.surname}
</a>
</li>
))}
<Footer />
</div>
);
}
}
const mapStateToProps = state => ({
adverts: state.adverts
});
export default connect(
mapStateToProps,
{ getAdverts }
)(Adverts);
我认为你的问题出在这里:
function mapStateToProps(state) {
return {
**adverts: state.adverts**
};
}
如果您 将 state.adverts 更改为 state.todos.adverts:
,它应该会起作用
function mapStateToProps(state) {
return {
adverts: state.todos.adverts
};
}
因为你的 reducer 叫做 todos,它有状态 { adverts },这就是为什么你不能访问广告,即使它们已经被获取。
您可以在此处查看工作版本:https://codesandbox.io/s/olqxm4mkpq
问题是,当你只用一个 reducer 创建一个 store 而不使用 combine reducer 时,可以直接在 ContainerS 中引用它,就像这样:
const mapStateToProps = state => {
return{
*name of var*: state.adverts /*direct refers to adverts*/
}
}
但是,当它使用 combined-reducer 时,它必须引用您想要 use.like 的确切减速器:
const mapStateToProps = state => {
return{
*name of var* : state.todos.adverts (indirect refers to adverts from combined-reducer todos)
}
}
without redux it works so that not a api connection problem
我有一个连接到代理的快速应用程序我已经设法在 React 中显示我的数据,但现在我想在 redux 中制作它 soo:
这是我的问题,我已经制作了所有 reducers/action、store 和 combine reducer,但是 我没有在我的页面中看到任何数据 我没有任何错误
这是我的代码:
Action
export const api = ext => `http://localhost:8080/${ext}`;
//
// ─── ACTION TYPES ───────────────────────────────────────────────────────────────
//
export const GET_ADVERTS = "GET_ADVERTS";
export const GET_ADVERT = "GET_ADVERT";
//
// ─── ACTION CREATORS ────────────────────────────────────────────────────────────
//
export function getAdverts() {
return dispatch => {
fetch("adverts")
.then(res => res.json())
.then(payload => {
dispatch({ type: GET_ADVERTS, payload });
});
};
}
export function getAdvert(id) {
return dispatch => {
fetch(`adverts/${id}`)
.then(res => res.json())
.then(payload => {
dispatch({ type: GET_ADVERT, payload });
});
};
}
reducer
import { combineReducers } from "redux";
import { GET_ADVERTS, GET_ADVERT } from "../actions/actions";
const INITIAL_STATE = {
adverts: [],
advert: {}
};
function todos(state = INITIAL_STATE, action) {
switch (action.type) {
case GET_ADVERTS:
return { ...state, adverts: action.payload };
case GET_ADVERT:
return { advert: action.payload };
default:
return state;
}
}
const todoApp = combineReducers({
todos
});
export default todoApp;
index.js
//imports
const store = createStore(todoApp, applyMiddleware(thunk));
ReactDOM.render(
<Provider store={store}>
<BrowserRouter>
<App />
</BrowserRouter>
</Provider>,
document.getElementById("app")
);
My advertlist page :
//imports..
class Adverts extends Component {
componentDidMount() {
this.props.getAdverts();
}
render() {
const { adverts = [] } = this.props;
return (
<div>
<Header />
<h1>Adverts</h1>
{adverts.map(advert => (
<li key={advert._id}>
<a href={"adverts/" + advert._id}>
{advert.name} {advert.surname}
</a>
</li>
))}
<Footer />
</div>
);
}
}
const mapStateToProps = state => ({
adverts: state.adverts
});
export default connect(
mapStateToProps,
{ getAdverts }
)(Adverts);
我认为你的问题出在这里:
function mapStateToProps(state) {
return {
**adverts: state.adverts**
};
}
如果您 将 state.adverts 更改为 state.todos.adverts:
,它应该会起作用function mapStateToProps(state) {
return {
adverts: state.todos.adverts
};
}
因为你的 reducer 叫做 todos,它有状态 { adverts },这就是为什么你不能访问广告,即使它们已经被获取。
您可以在此处查看工作版本:https://codesandbox.io/s/olqxm4mkpq
问题是,当你只用一个 reducer 创建一个 store 而不使用 combine reducer 时,可以直接在 ContainerS 中引用它,就像这样:
const mapStateToProps = state => {
return{
*name of var*: state.adverts /*direct refers to adverts*/
}
}
但是,当它使用 combined-reducer 时,它必须引用您想要 use.like 的确切减速器:
const mapStateToProps = state => {
return{
*name of var* : state.todos.adverts (indirect refers to adverts from combined-reducer todos)
}
}