[英]Changing the state of one component from another component in 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.