当redux更改商店中的状态时,我的reactJS应用程序不会重新呈现

时间:2019-05-11 13:55:17

标签: reactjs react-native redux react-redux reactive-programming

我是学习Redux的新手。 当我编写store.subscribe(render)时,我的应用程序编译没有错误,但没有任何显示。 我一直在关注文章https://medium.freecodecamp.org/understanding-redux-the-worlds-easiest-guide-to-beginning-redux-c695f45546f6),它很好地解释了redux,但是订阅部分却无法正常工作。 在这方面的任何帮助将不胜感激。

我尽我所能重建应用程序,但是没有运气。

index.js

./reducer/index.js

./store/index.js

App.js

3 个答案:

答案 0 :(得分:0)

您需要在商店中添加提供商以订阅更改。 有关更多详细信息,请检查此链接https://react-redux.js.org/api/provider

请找到商店的示例代码。

import React from 'react'
import ReactDOM from 'react-dom'
import { Provider } from 'react-redux'

import { App } from './App'
import createStore from './createReduxStore'

const store = createStore()

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
)

答案 1 :(得分:0)

您必须导入createStore。

import { createStore } from 'redux';

然后,您需要使store变量并传入化简器。

const store = createStore(yourReducer);

然后,减速器将如下所示。

const initState = {

}

const yourReducer = (state = initState, action) => {
    return state;
}

export default yourReducer

答案 2 :(得分:0)

首先,尝试将App.js的第17行更改为

<HelloWorld tech={this.state.tech}/>

收件人:

<HelloWorld tech={store.getState().tech}/>

否则,请尝试将构造函数放入您的App组件中:

constructor(props) {
    super(props);
    store.subscribe(() => {
      this.setState({
        tech: store.getState().tech
      });
    });
  }