Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

获取AppleItem的问题(换一种方式失效了) #4

Open
ckinmind opened this issue Feb 7, 2017 · 1 comment
Open

获取AppleItem的问题(换一种方式失效了) #4

ckinmind opened this issue Feb 7, 2017 · 1 comment

Comments

@ckinmind
Copy link
Owner

ckinmind commented Feb 7, 2017

之前渲染AppleItem的方式是,在render的return中

<div className="appleList">
                    {
                        apples.map(apple => {
                            if(!apple.isEaten)
                                return <AppleItem apple={apple} 
                                                               eatApple={actions.eatApple} 
                                                               key={apple.id} />
                        })
                    }
 </div>

这时候都是没问题的,吃苹果的操作都能进行

但是当我将其抽成一个方法的时候,发现吃苹果的操作失效了

 getAppleItem(){
        return this.props.apples.map(apple => {
            if(!apple.isEaten){
                return  <AppleItem apple={apple}  eatApple={actions.eatApple}  key={apple.id} />
            }
        });
    }

render(){
...
return (
   ...
 <div className="appleList">
       { this.getAppleItem() }
   </div>
  ...
)
 

}

测试的发现eatApple失效了了,不知道为什么

@ckinmind
Copy link
Owner Author

ckinmind commented Feb 7, 2017

找到失败的原因了,是因为,我在开头引入了actions, 然后再render中也有actions

import actions from '../actions/appleAction';

所以要修改getAppleItem中的actions来源

getAppleItem(){
        return this.props.apples.map(apple => {
            if(!apple.isEaten){
                return  <AppleItem apple={apple}  eatApple={this.props.actions.eatApple}  key={apple.id} />
            }
        });
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant