如何从json文件中获取数据?

时间:2016-12-27 11:26:48

标签: reactjs react-router react-redux redux-thunk

我正在使用redux-thunk从json文件中获取数据。我遵循此网址

https://github.com/gaearon/redux-thunk 我收到此错误

中间件不是功能

请告诉我如何从json文件中获取数据并在组件中显示

这是我的代码 https://plnkr.co/edit/R6TCNcK4kUaRkTDpObQN?p=preview

const {thunk} =ReduxThunk;
const abc= (state=0,action) => {
  console.log('in redux', action.type)
  switch(action.type){
    case 'INC':

      return state +1
    case 'DEC':
      return state -1
      default :
      return state;
  }
}
const {createStore,bindActionCreators ,applyMiddleware } =Redux;
const {Provider,connect} =ReactRedux;

const store = createStore(abc,
applyMiddleware(thunk)
);


class First extends React.Component {
  constructor (props){
    super(props);
    this.state ={
    digit :0  
    }
  }
  inc (){
    console.log('ince', this.props)
    this.props.increment();
  }

  dec (){
    console.log('dec')
    this.props.decrement();
  }
  getDate(){

  }
  render(){
    return (
    <div>
        <button onClick={this.inc.bind(this)}>INCREMENT</button>
        <p>{this.props.digit}</p>
        <button onClick={this.dec.bind(this)}>DECREMENT</button>
        <button onClick={this.getDate.bind(this)}>GET DATA</button>
      </div>
    )
  }
} 

const actions = {
    increment: () => {
        return {
            type: 'INC',
        }
    },
     decrement: () => {
        return {
            type: 'DEC',
        }
    }
};

const AppContainer = connect(
    function mapStateToProps(state) {
        return {
            digit: state
        };
    },
    function mapDispatchToProps(dispatch) {
        return bindActionCreators(actions, dispatch);
    }
)(First);
ReactDOM.render(
   <Provider store={store}>
    <AppContainer/>
  </Provider>
  ,document.getElementById('root'))

1 个答案:

答案 0 :(得分:0)

你快到了。

用这个替换第一行:

const thunk = ReduxThunk.default;

他们改变了出口方式并在github readme

上提到了出口方式