我有问题。 我的应用程序现在可以工作,但不像我想要的那样。
App.js
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
data: [],isLoading: true,};
}
componentDidmount() {
fetch('https://reactnative.dev/movies.json')
.then((response) => response.json())
.then((json) => {
this.setState({ data: json.movies });
})
.catch((error) => console.error(error))
.finally(() => {
this.setState({ isLoading: false });
});
}
render() {
const { data,isLoading } = this.state;
const goToPageTwo = () => actions.sw({text: 'Hello World!'});
return (
<View style={{ flex: 1,padding: 24 }}>
{isLoading ? <activityIndicator/> : (
<flatList
data={data}
keyExtractor={({ id },index) => id}
renderItem={({ item }) => (
<TouchableOpacity onPress={() => { actions.hp({text: item.id}) }}>
<Text>{item.title}</Text>
</TouchableOpacity>
)}
/>
)}
</View>
);
}
};
index.js / Route.js在我的情况下是sm.js
import app from './App';
import sw from './StarWars'
import bttf from './BacktotheFuture'
import hP from './handlePage';
import tM from './theMatrix';
import iN from './Inception';
const SceneManager = () => (
<Router>
<Scene>
<Scene key='home'
component={app}
title='BTour'
initial
/>
<Scene key='sw'
component={sw}
title='star wars'
/>
<Scene key='hp'
component={hP}
title='BTour'
/>
<Scene key='bttf'
component={bttf}
title='Back to the future'
/>
<Scene key='tM'
component={tM}
title='MAtrix'
/>
<Scene key='iN'
component={iN}
title='Inception'
/>
</Scene>
</Router>
)
export default SceneManager;
和我的Handlepage.js
import StarWars from './StarWars';
import BTTF from './BacktotheFuture';
import TM from './theMatrix';
import IN from './Inception';
export default class handlePage extends Component {
renderElement() {
if(this.props.text ==1) {
return <StarWars/>
}
if (this.props.text ==2) {
return <BTTF/>
}
if (this.props.text ==3) {
return <TM/>
}
if(this.props.text == 4) {
return <IN/>
}
return null;
}
render(){
return(
this.renderElement()
)
}
};
现在我的问题: 我想使用我在Router类中定义的场景。例如,当我按下主屏幕上的第一个按钮时,将打开“星球大战”,但不会打开我在路由类中编写的组件。
我可以将组件场景从Route.js移到If-Statemant的HandlePage.js中,还是可以将If Statemant放在Router类中。
现在,只有IF-Statemant中的脚本会打开。