简体   繁体   English

React Redux-更改未反映在组件中

[英]React Redux - changes aren't reflected in component

I created a simple React component for generating menu, and I wanted to replace it's visibility toggling with Redux (instead of state). 我创建了一个用于生成菜单的简单React组件,我想用Redux(而不是状态)替换它的可见性切换。

My component looks like this: 我的组件看起来像这样:

class SiteMenu extends React.Component {
  constructor(props) {
    super(props);
  }

  toggle(force = false) {
    let active = !active;

    store.dispatch({
      type: 'TOGGLE',
      active
    });
  }

  render() {
    const wrapperClass = this.props.active ? `${this.props.className}__wrapper ${this.props.className}__wrapper--active` : `${this.props.className}__wrapper`;
    return (
      <nav className={this.props.className} ref="nav">
        <button className={`${this.props.className}__trigger`} onClick={this.toggle.bind(this)}>
          {this.props.active}
        </button>
        <ul className={wrapperClass}>
        </ul>
      </nav>
    );
  }
}

I added mapStateToProps : 我添加了mapStateToProps

const mapStateToProps = (store) => {
  return {
    active: store.menuState.active
  }
};

and connect connect

connect(mapStateToProps)(SiteMenu);

My store: 我的商店:

import { createStore } from 'redux';
import reducers from './reducers/index.js';

const store = createStore(reducers, window.devToolsExtension && window.devToolsExtension());
export default store;

and reducers: 和减速器:

import { combineReducers } from 'redux';
import menuReducer from './menu-reducer';

const reducers = combineReducers({
  menuState: menuReducer
});

export default reducers;

const initialMenuState = {
  active: false
};

const menuReducer = (state = initialMenuState, action) => {
  switch(action.type) {
    case 'TOGGLE':
      console.log(action);
      return Object.assign({}, state, { active: action.active });
  }

  return state;
};

export default menuReducer;

When I check my Redux DevTools, state is changing. 当我检查Redux DevTools时,状态正在改变。 What should I do? 我该怎么办?

Code in the repo: https://github.com/tomekbuszewski/react-redux 仓库中的代码: https : //github.com/tomekbuszewski/react-redux

to use connect func , also you should add Provider from react-redux 要使用connect func,还应该从react-redux添加Provider

render(<Provider store={store}><Parent /></Provider>, app);

then you should add wrapped component to Parent component 那么您应该将包装的组件添加到父组件

const SiteMenuWrapped = connect(mapStateToProps)(SiteMenu);

///in Parent component
<Header className="site-header">
      <SiteMenuWrapped 
        className="site-navigation"
        content={this.state.sections}
      />
</Header>

Few issues: 几个问题:

  1. connect returns a higher order component, so best advice is to split out each component to a separate file and export the result of connect . connect返回一个更高阶的组件,因此最好的建议是将每个组件拆分到一个单独的文件中,并导出connect的结果。 See the examples at eg http://redux.js.org/docs/basics/ExampleTodoList.html . 请参见例如http://redux.js.org/docs/basics/ExampleTodoList.html上的示例。 This means that your mapStateToProps function is never being called, hence why this.props.active is always undefined. 这意味着您的mapStateToProps函数永远不会被调用,因此为什么this.props.active始终是未定义的。
  2. Your store registration is incorrect, the second parameter to createStore is the initial state. 您的商店注册不正确, createStore的第二个参数是初始状态。 To register the Chrome Redux dev tools see https://github.com/zalmoxisus/redux-devtools-extension 要注册Chrome Redux开发人员工具,请参见https://github.com/zalmoxisus/redux-devtools-extension
  3. You should use <Provider> to make the store available to all components. 您应该使用<Provider>使商店可用于所有组件。 See the Redux docs. 请参阅Redux文档。
  4. You can dispatch actions through this.props.dispatch or use mapDispatchToProps , see https://github.com/reactjs/react-redux/blob/master/docs/api.md 您可以通过this.props.dispatch调度动作,也可以使用mapDispatchToProps ,请参阅https://github.com/reactjs/react-redux/blob/master/docs/api.md

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

相关问题 尽管新状态显示在 React/Redux 的控制台中,但道具更改并未反映在组件 UI 上 - Props changes arenot reflected on component UI although the new state is shown in console in React/Redux 当 redux state 更改时,React 组件不会更新 - React component not updating when redux state changes 当 redux state 更改时更新反应组件 - Updating react component when redux state changes 如果不调用$ scope,则对$ scope属性的AngularJS更改不会反映在视图中。 - AngularJS changes to the $scope properties aren't reflected in the view without calling $scope.$apply React-Redux组件不反映Redux Store中的更改 - React-Redux component does not reflect changes in Redux Store React / Redux / Reselect - 即使在看到reducer中反映的更改后,mapStateToProps也会停止触发。 - React/Redux/Reselect - mapStateToProps stops firing even after seeing changes reflected in the reducer. react 组件已连接,redux 状态发生变化……但组件没有更新? - react component connected, redux state changes… but no update to component? React + Redux:Reducer不会重新加载组件 - React + Redux : reducer won't reload component 反应:即使 state 没有变化,也要重新渲染 - React: re-render even if there aren't changes in state 反应和还原,从组件中的存储更改更新状态 - react and redux, update state from store changes in a component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM