简体   繁体   English

来自子组件的SetState在React Native上失败

[英]SetState from Child Component fails on React Native

I have a Child component which triggers a function in the Parent component. 我有一个Child组件,它在Parent组件中触发了一个功能。 Although the function is triggered, the setState method seems to throw the following error. 尽管该函数被触发,但setState方法似乎引发以下错误。

Possible Unhandled Promise Rejection (id: 0): undefined is not an object (evaluating '_this12.setState') 可能的未处理的承诺拒绝(id:0):未定义不是对象(评估'_this12.setState')

Child Component: 子组件:

return (
    <View style={styles.buttonWrapper}>
        <View style={styles.socialMediaButtonWrapper}>
            <TouchableHighlight style={styles.socialMediaButtonBackFill} onPress={() => {
                this.props.onFacebookAuthenticationPressed()
            }}>
                <View style={[styles.socialMediaButtonFill, {backgroundColor: '#5158bb'}]}>
                    <FontAwesome style={styles.socialMediaButtonTextIconFill} name="facebook" size={20}/>
                </View>
            </TouchableHighlight>
        </View>
    </View>
);

Parent Component (Child Including Code): 父项(包括代码的子项):

<FacebookLoginComponent
    facebookAccessToken={this.state.facebookAccessToken}
    onFacebookAuthenticationPressed = {this.onFacebookAuthenticationPressed}
/>

Function: 功能:

onFacebookAuthenticationPressed: function () {
    LoginManager.logInWithReadPermissions(['public_profile', 'email']).then(function (result) {
        if (result.isCancelled) {
            console.log("Login Cancelled");
        } else {
            console.log("Login Successful");
            AccessToken.getCurrentAccessToken().then(
                (data) => {
                    let accessToken = data.accessToken;
                    console.log(accessToken);
                    this.setState({
                        facebookAccessToken: accessToken
                    })
                })
        }
    }, function (error) {
        console.log("some error occurred!!");
    }).done(() => {
        this.markAsComplete();
    })
},

I'm using react-native-fbsdk library for Authentication. 我正在使用react-native-fbsdk库进行身份验证。 The token gets printed from the console.log , but the setState() fails. console.log打印令牌,但是setState()失败。

Why is this? 为什么是这样? What's the workaround? 解决方法是什么?

Small fix for the child component (not neccessary, but better): 子组件的小修正(不是必需的,但是更好):

return (
    <View style={styles.buttonWrapper}>
        <View style={styles.socialMediaButtonWrapper}>
            <TouchableHighlight style={styles.socialMediaButtonBackFill} onPress={this.props.onFacebookAuthenticationPressed}>
                <View style={[styles.socialMediaButtonFill, {backgroundColor: '#5158bb'}]}>
                    <FontAwesome style={styles.socialMediaButtonTextIconFill} name="facebook" size={20}/>
                </View>
            </TouchableHighlight>
        </View>
    </View>
);

constructor of parent component: 父组件的构造函数:

this.onFacebookAuthenticationPressed = this.onFacebookAuthenticationPressed.bind(this);

I would not recommend to use bind in the render function. 我不建议在render函数中使用bind。 You should really use ES6 though, react native comes with support for that already. 尽管您应该真正使用ES6,但已经提供了对本机的支持。 Class properties instead of bind: 类属性而不是绑定:

onFacebookAuthenticationPressed = () => {
    LoginManager.logInWithReadPermissions(['public_profile', 'email']).then((result) => {
        ...
    }, function (error) {
        console.log("some error occurred!!");
    }).done(() => {
        this.markAsComplete();
    })
};

...or with ES5: ...或使用ES5:

onFacebookAuthenticationPressed: function() {
    LoginManager.logInWithReadPermissions(['public_profile', 'email']).then(function (result) {
        ...
    }.bind(this), function (error) {
        console.log("some error occurred!!");
    }).done(function() {
        this.markAsComplete();
    }.bind(this));
};
onFacebookAuthenticationPressed: function () {
    LoginManager.logInWithReadPermissions(['public_profile', 'email']).then((result) => {
        if (result.isCancelled) {
            console.log("Login Cancelled");
        } else {
            console.log("Login Successful");
            AccessToken.getCurrentAccessToken().then(
                (data) => {
                    let accessToken = data.accessToken;
                    console.log(accessToken);
                    this.setState({
                        facebookAccessToken: accessToken
                    })
                })
        }
    }, function (error) {
        console.log("some error occurred!!");
    }).done(() => {
        this.markAsComplete();
    })
},

Notice the result => here, which will pass this to your callback 注意result =>在这里,它将传递给您的回调

add bind(this) to the function .... 将bind(this)添加到函数中...

 <FacebookLoginComponent facebookAccessToken={this.state.facebookAccessToken} onFacebookAuthenticationPressed = {this.onFacebookAuthenticationPressed.bind(this)}// solution /> 

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

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