【问题标题】:REACT get data from nodeJS crud using reduxREACT 使用 redux 从 nodeJS crud 获取数据
【发布时间】:2018-06-08 11:48:44
【问题描述】:

我有一个快速应用程序已连接以与代理作出反应 我已经设法在反应中显示我的数据,但现在我想在 redux soo 中显示:

这是我的问题,我已经完成了所有的减速器/动作和存储,但我没有在我的页面中看到任何数据

这是我的代码:

行动:

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")
);

我的广告列表页面:

//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);

谢谢:)

【问题讨论】:

  • 你可以看看 devtools 的控制台,看看有没有错误
  • 在 devtools 的控制台中没有任何错误 /:

标签: node.js mongodb reactjs express redux


【解决方案1】:

在你需要的页面中,你应该喜欢

  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)

在 mapstatetoprops 中你从 redux 返回你的值

【讨论】:

  • 更新后:我尝试使用 componentDidMount 和 componentWillMount
猜你喜欢
  • 2021-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-12
  • 1970-01-01
  • 2018-11-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多