Node.js 使用redux从nodeJS crud获取数据
我有一个连接到proxy的express应用程序 我已经设法在react中显示我的数据,但现在我想在redux-soo中显示: 这是我的问题,我已经制作了所有的还原器/操作和存储,但是我在我的页面中没有看到任何数据 这是我的代码: 行动:Node.js 使用redux从nodeJS crud获取数据,node.js,mongodb,reactjs,express,redux,Node.js,Mongodb,Reactjs,Express,Redux,我有一个连接到proxy的express应用程序 我已经设法在react中显示我的数据,但现在我想在redux-soo中显示: 这是我的问题,我已经制作了所有的还原器/操作和存储,但是我在我的页面中没有看到任何数据 这是我的代码: 行动: export const api = ext => `http://localhost:8080/${ext}`; // // ─── ACTION TYPES ─────────────────────────────────────────────
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 });
});
};
}
减速器:
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")
);
//导入。。。
const store=createStore(todoApp、applyMiddleware(thunk));
ReactDOM.render(
))}
);
}
}
常量mapStateToProps=状态=>({
广告:state.adverts
});
导出默认连接(
MapStateTops,
{getAdverts}
)(广告);
谢谢:)在您需要的页面中,您应该喜欢
import React , {Component} from 'react'
import { connect }from 'react-redux'
import { getAdvert } from '...action.js'
class example extends Component{
componentWillMount(){
this.props.getAdvert()
}
render(){
return(
<div>
hello
</div>
)
}
}
const mapStateToProps = ({}) => {
return{}
}
export default connect (mapStateToProps,{getAdvert})(example)
import React,{Component}来自“React”
从“react redux”导入{connect}
从“…action.js”导入{getAdvert}
类示例扩展组件{
组件willmount(){
this.props.getAdvert()
}
render(){
返回(
你好
)
}
}
常量mapStateToProps=({})=>{
返回{}
}
导出默认连接(mapStateToProps,{getAdvert})(示例)
在MapStateTops中,您从redux返回您的值更新后:我已尝试使用componentDidMount和ComponentWillMount。您可以查看devtools的控制台,看看是否有一些错误devtools的控制台中没有任何错误/:
import React , {Component} from 'react'
import { connect }from 'react-redux'
import { getAdvert } from '...action.js'
class example extends Component{
componentWillMount(){
this.props.getAdvert()
}
render(){
return(
<div>
hello
</div>
)
}
}
const mapStateToProps = ({}) => {
return{}
}
export default connect (mapStateToProps,{getAdvert})(example)