Should I use useselector/useDispatch instead of mapStateToProps

后端 未结 5 1408
孤街浪徒
孤街浪徒 2021-02-19 00:07
5条回答
  •  孤街浪徒
    2021-02-19 00:40

    I think you misunderstand what "top level" is. It merely means that, inside a functional component, useSelector() cannot be placed inside loops, conditions and nested functions. It doesn't have anything to do with root component or components structure

    // bad
    const MyComponent = () => {
      if (condition) {
        // can't do this
        const data = useSelector(mySelector);
        console.log(data);
      }
    
      return null;
    }
    
    ---
    
    // good
    const MyComponent = () => {
      const data = useSelector(mySelector);
    
      if (condition) {
        console.log(data); // using data in condition
      }
    
      return null;
    }
    

    If anything, mapStateToPtops is located at even higher level than a hook call

    the rules of hooks make it very hard to use that specific hook. You still need to somehow access a changing value from the state inside callbacks

    To be fair you almost never have to access changing value inside a callback. I can't remember last time I needed that. Usually if your callback needs the latest state, you are better off just dispatching an action and then handler for that action (redux-thunk, redux-saga, redux-observable etc) will itself access the latest state

    This is just specifics of hooks in general (not just useSelector) and there are tons of ways to go around it if you really want to, for example

    const MyComponent = () => {
      const data = useSelector(mySelector);
      const latestData = useRef()
      latestData.current = data
    
      return (
        

    What are the benefits of using the hook besides saving lines of code compared to mapStateToProps?

    1. You save time by not writing connect function any time you need to access store, and removing it when you no longer need to access store. No endless wrappers in react devtools
    2. You have clear distinction and no conflicts between props coming from connect, props coming from parent and props injected by wrappers from 3rd party libraries
    3. Sometimes you (or fellow developers you work with) would choose unclear names for props in mapStateToProps and you will have to scroll all the way to mapStateToProps in the file to find out which selector is used for this specific prop. This is not the case with hooks where selectors and variables with data they return are coupled on the same line
    4. By using hooks you get general advantages of hooks, the biggest of which is being able couple together and reuse related stateful logic in multiple components
    5. With mapStateToProps you usually have to deal with mapDispatchToProps which is even more cumbersome and easier to get lost in, especially reading someone else's code (object form? function form? bindActionCreators?). Prop coming from mapDispatchToProps can have same name as it's action creator but different signature because it was overridden in mapDispatchToprops. If you use one action creator in a number of components and then rename that action creator, these components will keep using old name coming from props. Object form easily breaks if you have a dependency cycle and also you have to deal with shadowing variable names

    .

    import { getUsers } from 'actions/user'
    
    class MyComponent extends Component {
      render() {
        // shadowed variable getUsers, now you either rename it
        // or call it like this.props.getUsers
        // or change import to asterisk, and neither option is good
        const { getUsers } = this.props
        // ...
      }
    }
    
    const mapDispatchToProps = {
      getUsers,
    }
    
    export default connect(null, mapDispatchToProps)(MyComponent)
    

提交回复
热议问题