【问题标题】:React Redux - changes aren't reflected in componentReact Redux - 更改不会反映在组件中
【发布时间】:2016-07-01 11:55:48
【问题描述】:

我创建了一个简单的 React 组件来生成菜单,我想用 Redux(而不是状态)替换它的可见性切换。

我的组件如下所示:

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>
    );
  }
}

我加了mapStateToProps

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

connect

connect(mapStateToProps)(SiteMenu);

我的商店:

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

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

和减速器:

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;

当我检查我的 Redux DevTools 时,状态正在改变。我该怎么办?

回购代码:https://github.com/tomekbuszewski/react-redux

【问题讨论】:

    标签: javascript reactjs redux react-redux


    【解决方案1】:

    要使用 connect func ,你还应该从 react-redux 添加 Provider

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

    那么你应该将包装的组件添加到父组件

    const SiteMenuWrapped = connect(mapStateToProps)(SiteMenu);
    
    ///in Parent component
    <Header className="site-header">
          <SiteMenuWrapped 
            className="site-navigation"
            content={this.state.sections}
          />
    </Header>
    

    【讨论】:

    • 谢谢,它可以工作 ;-) 我不明白为什么我需要一个包装器。
    • 因为“connect”正在连接到商店。否则你无法访问 mapStateToProps。而且您没有从商店获取数据
    • 顺便说一句,您不需要在组件名称中“包装”
    • 我想通了 ;-)
    【解决方案2】:

    几个问题:

    1. connect 返回一个更高阶的组件,因此最好的建议是将每个组件拆分到一个单独的文件中并导出 connect 的结果。请参阅示例,例如http://redux.js.org/docs/basics/ExampleTodoList.html。这意味着您的 mapStateToProps 函数永远不会被调用,因此 this.props.active 始终未定义。
    2. 你的店铺注册不正确,createStore的第二个参数是初始状态。要注册 Chrome Redux 开发工具,请参阅 https://github.com/zalmoxisus/redux-devtools-extension
    3. 您应该使用&lt;Provider&gt; 使商店可用于所有组件。请参阅 Redux 文档。
    4. 您可以通过this.props.dispatch 或使用mapDispatchToProps 调度操作,请参阅https://github.com/reactjs/react-redux/blob/master/docs/api.md

    【讨论】:

    • 实际上,当我观看 DevTools 时,它正在从 true 变为 false。但是,我的问题是更改没有反映在我的组件中。我有这个代码:const wrapperClass = this.props.active ? ${this.props.className}__wrapper ${this.props.className}__wrapper--active`:${this.props.className}__wrapper; ` 并且它始终是 false,即使 DevTools 说它应该是 true
    • 你是说wrapperClass 的值仍然是__wrapper 而不是__wrapper--active,因为this.props.active 总是假的?
    • 总是__wrapper。但是登录控制台this.props 只会返回我手动放在那里的道具。当我添加 active 属性时,它也没有改变。
    • 谢谢。出于某种原因,我没有看到您发布的 Redux 示例,我会尽快完成它们;-)
    猜你喜欢
    • 2018-12-03
    • 2021-11-16
    • 2017-06-10
    • 2021-11-15
    • 1970-01-01
    • 2020-03-25
    • 1970-01-01
    • 2020-03-03
    • 1970-01-01
    相关资源
    最近更新 更多