简体   繁体   English

Redux容器不知道什么是“ this”

[英]Redux container does not know what “this” is

I have been trying to figure out why my container does not know what "this" is. 我一直在试图弄清楚为什么我的容器不知道“这个”是什么。

If I change the container to a component then the it all works correctly. 如果我将容器更改为组件,则一切正常。

This Component works perfectly and changes the state when it hits the store 该组件可以完美运行并在到达商店时更改状态

    class testCard extends Component {


       test= (event) => {
           console.log("!!!!!!!!!!!!!!"); // Shows
           this.props.testAction(); // This works
       }

       render(){
       return (
       <Card>
           <CardActionArea onClick={this.test}>
               ... // Card stuff
           </CardActionArea>
           <CardActions>
       </Card>)
       }
   }


   const mapStateToProps = (state) => {
       return {

      }
   }

   const mapDispatchToProps = (dispatch) => {
       return bindActionCreators(
       {
            testAction:   testAction()
        }, dispatch)
   }
   export default connect(mapStateToProps, mapDispatchToProps) (testCard);

Below the code does not know what 'this' is and will throw an error. 下面的代码不知道什么是“这”,并且会引发错误。

   const testCard = (props) => {

    let test= (event) => {
        console.log("!!!!!!!!!!!!!!"); // Shows
        this.props.testAction(); // This errors saying cannot find props of undefined
    }


    return (
    <Card>
        <CardActionArea onClick={test}>
            ... // Card stuff
        </CardActionArea>
        <CardActions>
    </Card>)
  }


  const mapStateToProps = (state) => {
     return {

     }
  }

  const mapDispatchToProps = (dispatch) => {
     return bindActionCreators({
         testAction:  testAction()
     }, dispatch)
   }
   export default connect(mapStateToProps, mapDispatchToProps) (testCard);

In short you have to call props.testAction() in your 2nd example. 简而言之,您必须在第二个示例中调用props.testAction()。 It uses ES6 arrow function. 它使用ES6箭头功能。 https://hackernoon.com/javascript-es6-arrow-functions-and-lexical-this-f2a3e2a5e8c4 https://hackernoon.com/javascript-es6-arrow-functions-and-lexical-this-f2a3e2a5e8c4

Also when you use arrow functions for react components you don't need the render() method (render is needed when you use class based component ie extends React.Component, then you need to implement it), all you need in arrow function is to return your result ie jsx data. 同样,当您将箭头函数用于React组件时,不需要render()方法(当您使用基于类的组件(即扩展React.Component时需要渲染),则需要实现它),在箭头函数中所需要做的就是返回您的结果,即jsx数据。

https://medium.com/@yassimortensen/container-vs-presentational-components-in-react-8eea956e1cea https://medium.com/@yassimortensen/container-vs-presentational-components-in-react-8eea956e1cea

A couple of notes. 几个注意事项。

   class testCard extends React.Component {

    test = (event) => {
        console.log("!!!!!!!!!!!!!!");
        this.props.testAction()
    }

    render(){
    return (
    <Card>
        <CardActionArea onClick={test}>
            ... // Card stuff
        </CardActionArea>
        <CardActions>
    </Card>)
    }
   }

  const mapStateToProps = (state) => {
     return {

     }
  }

  const mapDispatchToProps = (dispatch) => {
     return bindActionCreators({
         testAction:  testAction()
     }, dispatch)
   }
   export default connect(mapStateToProps, mapDispatchToProps) (testCard);

To give you further insight onto why "this" was giving you an error. 让您进一步了解为什么“ this”给您一个错误。 It's because "this" always points to the closest object. 这是因为“ this”始终指向最接近的对象。 Because you were not using a class-component (an object), it pointed to the global window object by default, which does not have a property of props . 由于未使用类组件(对象),因此默认情况下它指向全局窗口对象 ,该对象不具有props属性。 Which is why you were getting the error "cannot find props of undefined". 这就是为什么出现错误“找不到未定义的道具”的原因。 You can work around this by a class-component in combination with arrow functions. 您可以通过结合箭头功能的类组件来解决此问题。

If you want to use the props passed down to non-class component, you would simply use: 如果要使用传递给非类组件的道具,则只需使用:

props.nameOfValuePassedDown() instead of this.props.nameofValuePassedDown() props.nameOfValuePassedDown()代替this.props.nameofValuePassedDown()

It is important that you have to know what props is. 重要的是您必须知道什么是道具。 Most components can be customized when they are created, with different parameters. 大多数组件在创建时都可以使用不同的参数进行自定义。 These creation parameters are called props . 这些创建参数称为props。 With props we can send and receive data from component to component . 借助props,我们可以在组件之间发送和接收数据。 For enough understanding props, refer to react tutorial Tic-Tac-Toe game. 要获得足够的理解道具,请参考React Tic-Tac-Toe游戏教程。

I hope this will be your help. 希望这对您有所帮助。

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

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