简体   繁体   English

与Redux存储分开更新React组件状态

[英]Updating react component state separate from redux store

I have a react component which has a list of selectable items. 我有一个具有可选项目列表的react组件。 I want the list to be hidden until the user clicks a button to show the list. 我希望在用户单击按钮以显示列表之前隐藏该列表。

There is no reason for the 'showList' to be a part of the global store, as it only matters to this component. 没有必要让“ showList”成为全局存储的一部分,因为它仅与该组件有关。

I am using redux connect, and am having difficulting understanding how to access the local components this.setState as it is always undefined . 我正在使用redux connect,并且很难理解如何访问本地组件this.setState因为它始终是undefined

// Redux connect
import { connect } from 'react-redux'
import { setClockSpeed } from '../actions'
import SpeedControls from '../components/speedControl/SpeedControl'

let showList = false;
const mapStateToProps = (state) => {
  return {
    speed: state.clock.speed,
    speeds: [ 1, 5, 10, 25, 50, 100, 150, 200 ],
    showList
  }
}

const mapDispatchToProps = (dispatch) => {
  return {
    setClockSpeed: newSpeed => displatch(setClockSpeed(newSpeed)),
    toggleList: this.setState({showList: !showList}) //undefined here
  }
}

const PlayerSpeedControls = connect(
  mapStateToProps,
  mapDispatchToProps
)(SpeedControls)

export default PlayerSpeedControls

// Component
import React, { PropTypes } from 'react';

import style from './SpeedControl.css';


const getSpeedItemClass = (s, idx, speed, speeds) => {
  let speedClass = style.speedItem;
  if (idx === 0) speedClass += ' ' + style.lastSpeedItem;
  if (idx === speeds.length - 1 ) speedClass += ' ' + style.firstSpeedItem;
  if (s === speed) speedClass += ' ' + style.currentActiveSpeed;
  return speedClass
}



const SpeedControls = ({ setClockSpeed, toggleList, speed, speeds, showList }) => (
  <div className={style.speedControl}>
    <div className={style.speedList}>
      <ul className={showList ? style.speedOptions : style.hideSpeedOptions}>
        { speeds.map((s, idx) => {
          return <li key={idx} className={getSpeedItemClass(s, idx, speed, speeds)} onClick={(s) => { 
          }}>{s}x</li>
          })
        }
      </ul>
      <span className={style.currentSpeed} onClick={ toggleList || this.setState //undefined here too }>{speed}x</span>
    </div>
  </div>
);

SpeedControls.propTypes = {
  setClockSpeed: PropTypes.func.isRequired,
  speed: PropTypes.number.isRequired,
  speeds: PropTypes.array.isRequired
};

export default SpeedControls;

In the example provided, You are using react state less Component. 在提供的示例中,您所使用的反应状态较少的组件。 You cannot access state in such components. 您无法访问此类组件中的状态。 Use normal components to have the state in component. 使用普通组件将状态包含在组件中。

 class SpeedControls extends Component {
  render() {
  }
}

mapDispatchToProps is just utility function provided by react-redux, It is not a component.There is no way you can access or update state there. mapDispatchToProps只是react-redux提供的实用程序功能,它不是组件,无法访问或更新状态。

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

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