簡體   English   中英

反應本地渲染子組件

[英]React native render child component

我正在嘗試創建一個標題組件,該組件會根據您正在查看的屏幕而變化。 但是,如果我嘗試創建一些動態按鈕,則子組件將無法工作。

我已將標題的每個部分拆分為較小的組件,問題是除非我自己添加每個按鈕,否則標題HeaderRightSide的右側不會呈現。

const screenIcon = {
    Home: 'home',
    Settings: 'tools'
}


export class AppHeader extends Component {
    static propTypes = {
        screenName: PropTypes.string.isRequired,
        navigation: PropTypes.object.isRequired,
        googleUser: PropTypes.object.isRequired,
        UserSignIn_logOut: PropTypes.func.isRequired
    }
    signOut = async () => {
        try {
            await GoogleSignin.revokeAccess();
            await GoogleSignin.signOut();
        } catch (error) {
            // console.error(error);
        } finally {
            this.props.UserSignIn_logOut({});
            this.props.navigation.navigate('SignIn');
        }
    }
    componentDidMount() {
        console.log(this.props.navigation.state)
    }
    render() {
        return (
            <Header>
                <HeaderLeftSide screenName={this.props.screenName} />
                <HeaderBody screenName={this.props.screenName} />
                <HeaderRightSide screenName={this.props.screenName} navigation={this.props.navigation} />
            </Header>
        )
    }
}

HeaderLeftSide = (props) => (
    <Left>
        <Button transparent>
            <Icon name={screenIcon[props.screenName]} />
        </Button>
    </Left>
)

HeaderBody = (props) => (
    <Body>
        <Title>{props.screenName}</Title>
    </Body>
)

HeaderRightSide = (props) => (
    <Right>
        {Object.keys(screenIcon).forEach(screen => 
           ( <Button transparent>
            <Icon name={screenIcon[screen]} />
        </Button>)
        )}
    </Right>
)


const mapStateToProps = (state) => ({
    googleUser: state.UserSignInReducer.googleUser
})

const mapDispatchToProps = {
    UserSignIn_logOut: () => {
        dispatch(UserSignIn_logOut());
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(AppHeader)

我只需要更改每個地圖即可。 正確的代碼:

HeaderRightSide = (props) => (
    <Right>
        {Object.keys(screenIcon).map(screen => (
            <Button transparent>
                <Icon name={screenIcon[screen]} />
            </Button>
        )
        )}
    </Right>
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM