简体   繁体   English

反应和还原,从组件中的存储更改更新状态

[英]react and redux, update state from store changes in a component

I'm trying to update my home component state by getting data from the redux store every time the store is updated. 我试图通过每次存储更新时从redux store获取数据来更新home component状态。 I'm not sure what's wrong with the code below. 我不确定下面的代码有什么问题。 I can't listen to store changes in my `home component. 我听不到我的`home组件中存储的更改。

my dispatch function is handled in this class. 我的调度功能在此类中处理。

export class GanttFilter extends Component {
...
    handleSubmit = () => {
        this.gFilter.filterGanttData(this.formValues)
            .then((result) => {
                if (result.data)
                    this.props.dispatch(ganttActions.loadGanttData(result.data));
            });
    }

...
GanttFilter.propTypes = {
    dispatch: PropTypes.func.IsRequired
};

function mapStateToProps(state) {
    return {
        ganttData: state.gantt.ganttData
    };
}


export default connect(mapStateToProps)(GanttFilter);

What I would like to do every time dispatch is called and the data changes, is update the state in my home component . 每次调用时我想做的就是数据更改,这就是更新home component的状态。 Here is the component. 这是组件。

export class Home extends Component {

    constructor() {
        super();

        this.state = {
            data: [],
            links: []
        };
    }

    render() {
          return (
              <div className="fill">
                  <Gantt data={this.state.data} links={this.state.links} />
          </div>
        );
    }
}

Home.propTypes = {
    data: PropTypes.IsRequired
};
function mapStateToProps(state) {
    return {
        data: state.gantt.ganttData
    };
}

export default connect(mapStateToProps)(Home);

the function mapStateToProps is never hit when I set a break point. 设置断点时,不会命中mapStateToProps函数。 How can I listen to changes to the store from the home component and update state? 如何从home组件监听商店的更改并更新状态?

Edit: Here is the wrapper component 编辑:这是包装器组件

function renderApp() {
  // This code starts up the React app when it runs in a browser. It sets up the routing
  // configuration and injects the app into a DOM element.
  const baseUrl = document.getElementsByTagName("base")[0].getAttribute("href");
    ReactDOM.render(
        <ReduxProvider store={store}>
            <AppContainer>
              <BrowserRouter children={routes} basename={baseUrl} />
            </AppContainer>
        </ReduxProvider>,
    document.getElementById("react-app")
  );
}

reducers 减速器

const actionTypes = require('../actions/actionTypes');


const gantt = {
    ganttData: [],
    selectedTask: null
};

export default function ganttReducer(state = gantt, action) {
    switch (action.type) {
        case actionTypes.loadGanttData:
            return { ...state, ganttData: [...action.ganttData] };
        default:
            return state;
    }
}

root reducer 根减速机

import { combineReducers } from 'redux';
import gantt from './ganttReducer';

const rootReducer = combineReducers({
    gantt
});

export default rootReducer;

actions 行动

const actionTypes = require('./actionTypes');

export function loadGanttData(ganttData) {
    return { type: actionTypes.loadGanttData, ganttData };
}

export function getSelectedTask(ganttTask) {
    return { type: actionTypes.setSelectedTask, ganttTask };
}

Error: 错误: 在此处输入图片说明

Change this: 更改此:

render() {
      return (
          <div className="fill">
              <Gantt data={this.state.data} links={this.state.links} />
      </div>
    );
}

To

render() {
      return (
          <div className="fill">
              <Gantt data={this.props.data} links={this.state.links} />
      </div>
    );
}

Your data is comming from your props (redux), not from your state. 您的数据来自道具(redux),而不是您的州。

Make sure you import your Home component using import Home from '...' as opposed to import { Home } from '...' , otherwise you'd be grabbing the unconnected component. 确保使用import Home from '...'import Home from '...' Home组件,而不是import Home from '...' import { Home } from '...'import { Home } from '...'组件,否则您将抓住未连接的组件。 In general, I would also avoid exporting the unconnected component. 通常,我也将避免导出未连接的组件。

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

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