简体   繁体   English

在redux容器中使用store.getState()是否正确?

[英]Is correct to use store.getState() in a redux container?

I need to access a property in the store before dispatching an action, in the container (code shown below) I am accessing using it using store.getState() . 我需要先在存储中访问属性,然后再通过store.getState()使用该store.getState()访问容器(如下所示的代码store.getState()

I would like to know if it is correct or a more appropriate way exists. 我想知道这是正确的还是更合适的方法。

import { connect } from 'react-redux'
import LocationFinderSearch from './locationFinderSearch'
import { getLocations, setSearchValue } from './locationFinderSearchActions'
import store from '../app/store'

const mapStateToProps = (state, ownProps) => {
  return {

  }
}

const mapDispatchToProps = (dispatch, ownProps) => {
  return {
    onLocationSearchClick: (e) => {
      e.preventDefault()
      let value = store.getState().locationFinderReducer.locationFinder.ui.inputValue
      dispatch(getLocations(value))
    },
    onLocationInputChange: (e) => {
      e.preventDefault()
      let value = e.target.value
      dispatch(setSearchValue(value))
    }
  }
}

const LocationFinderSearchContainer = connect(
  mapStateToProps,
  mapDispatchToProps
)(LocationFinderSearch)

export default LocationFinderSearchContainer

I think your first instinct should be to map the value from you state to a component prop like this: 我认为您的第一个直觉应该是将状态值映射到组件属性,如下所示:

const getInputValue = state =>
  state.locationFinderReducer.locationFinder.ui.inputValue

const mapStateToProps = state => ({
  inputValue: getInputValue(state),
})

Now that you have the inputValue in the components props, the question is how to use in your dispatch functions? 现在,您在组件props中具有inputValue,问题是如何在您的调度函数中使用?

For this I would encourage you to put the immediate event handling (preventDefault and target.value) inside your component. 为此,我鼓励您将立即事件处理(preventDefault和target.value)放入组件内部。 This makes the mapDispatchToProps object much easier, puts the event handling encapsulated in your component and finally solves your problem of not using getState(). 这使mapDispatchToProps对象更加容易,将事件处理封装在组件中,并最终解决了不使用getState()的问题。

Your mapDispatchToProps becomes: 您的mapDispatchToProps变为:

import { getLocations, setSearchValue } from './locationFinderSearchActions'

const mapDispatchToProps = {
  getLocations,
  setSearchValue,
}

The LocationFinderSearch component has to handle the events now and has access to the inputValue in this.props.inputValue: LocationFinderSearch组件必须立即处理事件,并可以访问this.props.inputValue中的inputValue:

class LocationFinderSearch extends React.Component {

  ...

  onLocationSearchClick = (e) => {
    const { getLocations, inputValue } = this.props

    e.preventDefault()
    getLocations(inputValue)
  }

  onLocationInputChange = (e) => {
    const value = e.target.value
    const { setSearchValue } = this.props

    e.preventDefault()
    setSearchValue(value)
  }

  ...
}

I hope this answer will help you. 希望这个答案对您有帮助。

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

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