簡體   English   中英

React-native傳遞給另一個組件

[英]React-native passing component to another

我想將一個React組件傳遞給我的CustomTab組件。 所以我有這樣的事情:

const FirstRoute = () => <View style={[ styles.container, { backgroundColor: '#ff4081' } ]} />;
const SecondRoute = () => <View style={[ styles.container, { backgroundColor: '#673ab7' } ]} />; 

export default class Profile extends Component {
_renderScene = SceneMap({
    first: FirstRoute,
    second: SecondRoute,
});
render() {
return (
    <CustomTab scenes={this._renderScene}/>
);
}

在我的CustomTab中,我有:

export default class CustomTab extends React.Component {
_renderScene = this.props.scenes;

render() {

return (
    <TabViewAnimated
        style={styles.container}
        navigationState={this.state}
        renderScene={this._renderScene}

但這不起作用,看起來傳遞的參數是錯誤的。可以實現嗎? 感謝任何幫助。

您的語法錯誤,您需要在渲染器或constructor分配_renderScene變量,但是不需要將this.props.scenes分配給另一個類變量,您可以直接使用它

export default class CustomTab extends React.Component {
    render() {
        return (
            <TabViewAnimated
                style={styles.container}
                navigationState={this.state}
                renderScene={this.props. scenes}

您應該在構造函數或其他類似_renderScene組件生命周期的方法中設置_renderScene屬性。

constructor(props) {
    super(props);
    this._renderScene = this.props.scenes;
}

暫無
暫無

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

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