简体   繁体   English

React中的初始状态与Redux无法正常工作

[英]Initial state in React with Redux not working

Having problem with the initial state in React with Redux. 在React with Redux中遇到初始状态问题。 It's my first application to use async calls, and it seems that there is some stuff I can't figure out. 这是我的第一个使用异步调用的应用程序,似乎有一些我无法弄清楚的东西。

I'm using a spreadsheet from Google as my "backend", so it will be easy for my relatives to rent out our ski lodge. 我使用谷歌的电子表格作为我的“后端”,所以我的亲戚很容易出租我们的滑雪小屋。 It is just a spreadsheet of the weeks and if they are available or not. 它只是一个周的电子表格,如果它们可用或没有。

By looking at the console, the data gets fetched, but something is not working. 通过查看控制台,可以获取数据,但有些东西无效。

Here is the error message: 这是错误消息:

Error message 错误信息

Here is the github repo: https://github.com/Danielbook/kulan2016 这是github回购: https//github.com/Danielbook/kulan2016

And, here is the code: 而且,这是代码:

index.js index.js

import About from './components/About';
import Guest from './components/Guest';
import Booking from './containers/Booking';

import getSpreadsheetData from './actions'

const logger = createLogger();
const store = createStore(
  rootReducer, compose(
  applyMiddleware(thunk, promise, logger),
  window.devToolsExtension ? window.devToolsExtension() : f => f)
);

const tableUrl = "https://spreadsheets.google.com/feeds/list/1QxC20NcuHzqp1Fp7Dy2gpBDbJzN4YpmjiEcr7PSpsuM/od6/public/basic?alt=json";

store.dispatch(getSpreadsheetData(tableUrl));

// Create an enhanced history that syncs navigation events with the store
// const history = syncHistoryWithStore(browserHistory, store);

ReactDOM.render(
    <Provider store={store}>
      <Router history={hashHistory}>
        <Route path="/" component={App}>
          <IndexRoute component={Home}/>
          <Route path="/about" component={About}/>
          <Route path="/guest" component={Guest}/>
          <Route path="/booking" component={Booking}/>
        </Route>
      </Router>
    </Provider>,
    document.getElementById('app')
);

reducer-bookingdata.js 减速器bookingdata.js

const initialState = {
  spreadsheetData: [],
  loading: false,
  errorMsg: ''
};

export default function spreadsheet(state = initialState, action) {
  switch (action.type) {
    case 'SPREADSHEET_REQUEST':
      return {
        ...state,
        loading: true
      };

    case 'SPREADSHEET_RECEIVED':
      return {
        ...state,
        loading: false,
        spreadsheetData: action.payload.data,
        errorMsg: ''
      };

    case 'SPREADSHEET_FAIL':
      return {
        loading: false,
        errorMsg: action.payload.error
      };

    default:
      return state;
  }
}

booking-actions.js 预订-actions.js

import fetch from 'isomorphic-fetch';

// --- Action-creators ---
function requestSpreadsheet() {
  return {
    type: 'SPREADSHEET_REQUEST'
  }
}

function receiveSpreadsheet(data) {
  return {
    type: 'SPREADSHEET_RECEIVED',
    payload: {
      data: data
    }
  }
}

function receiveSpreadsheetError(error) {
  return {
    type: 'SPREADSHEET_FAIL',
    payload: {
      error: error
    }
  }
}

// --- API ---
function fetchTable(tableUrl) {
  // Code related to API here. Should just return a promise.
  return fetch(tableUrl);
}

// --- Thunks ---
export default function getSpreadsheetData(tableUrl) {
  return function (dispatch, getState) {
    // Tell reducers that you are about to make a request.
    dispatch(requestSpreadsheet());

    // Make the request, then tell reducers about
    // whether it succeeded or not.
    // Here, we update the app state with the results of the API call.
    return fetch(tableUrl)
      .then(response => response.json())
        .then(data => dispatch(receiveSpreadsheet(data.feed.entry)),
              error => dispatch(receiveSpreadsheetError(error)));
  }
}

Booking.js Booking.js

import React, {Component, PropTypes} from 'react';
import {connect} from 'react-redux';
import {Grid, Table} from 'react-bootstrap';
import TableRow from '../components/TableRow';

class Booking extends Component {
  constructor(props) {
    super(props);

  }
  render() {    
    return (
      <Grid fluid className="pageContainer">
        <h4>För att boka, skicka ett mail till oss <a href="mailto:boka@kulaniklappen.se">här!</a></h4>
        <p>{this.props.errorMsg}</p>
        <Table responsive>
          <thead>
          <tr>
            <th>Ledighet</th>
            <th>Vecka</th>
            <th>Datum</th>
            <th>Pris</th>
          </tr>
          </thead>
          <tbody>
            {this.props.spreadsheetData.map((row, i) => {
              return (
                <TableRow key={i} data={row} />
              )
            })}
          </tbody>
        </Table>
      </Grid>
    );
  }
}

const mapStateToProps = (state) => {
  console.log(state);
  return {
    spreadsheetData: state.spreadsheetData,
    loading: state.loading,
    errorMsg: state.errorMsg
  }
};

export default connect(mapStateToProps, null)(Booking);

As i can see from error message your state structure is 正如我从错误消息中看到的,您的状态结构是

{
    bookingData: {
        spreadsheetData: ...,
        loading: ...,
        errorMsg: ...
    },
    ...
}

And your mapStateToProps should be 你的mapStateToProps应该是

const mapStateToProps = (state) => {
    console.log(state);
    return {
        spreadsheetData: state.bookingData.spreadsheetData,
        loading: state.bookingData.loading,
        errorMsg: state.bookingData.errorMsg
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM