【发布时间】:2019-05-18 09:38:30
【问题描述】:
我感觉我可能走错了方向,
因为不可能这么不直观
但话说回来,我真的很笨。
所以我的问题是:
当前不在场景中时如何访问场景功能。
我的例子:
App.js
import React from 'react';
import {Text, View} from 'react-native';
import Home from './components/Home';
import Add from './components/Add';
import {Router, Scene, Actions} from 'react-native-router-flux';
class App extends React.Component {
constructor() {
super();
}
render() {
return(
<Router>
<Scene key='root'>
<Scene key='home' component={Home} title='Home' rightButtonImage={require('./assets/plus.png')} onRight={() => {Actions.add()}}/>
<Scene key='add' component={Add} title='Add'/>
</Scene>
</Router>
);
}
}
export default App;
Home.js
import React from 'react';
import {View, Text, ScrollView, SafeAreaView, Image, Dimensions} from 'react-native';
import ScrollCompanyIndex from './ScollCompanyIndex';
var {screenHeight, screenWidth} = Dimensions.get('window');
var companies = require('./companies.json');
var companiesString = JSON.stringify(companies);
var comapniesJson = JSON.parse(companiesString.toString());
var companyList = [];
class Home extends React.Component {
constructor() {
super();
companyList = [];
comapniesJson.companies.forEach(element => {
companyList.push(element);
});
}
rerenderComponent() {
console.log('Rerendering the component');
}
render() {
return(
<SafeAreaView style={{flex: 1}}>
<ScrollView>
{
companyList.map((item, key) => (
<ScrollCompanyIndex key={key} imageUri={'https://picsum.photos/' + (129 + Math.floor(Math.random() * 3)) + '/' + (129 + Math.floor(Math.random() * 3))} companyName={item.name} companyAddress={item.address + ', ' + item.city}/>
))
}
</ScrollView>
</SafeAreaView>
);
}
}
export default Home;
Add.js
import React from 'react';
import {View, Text, Button} from 'react-native';
import { Actions } from 'react-native-router-flux';
class Add extends React.Component {
render() {
return(
<View>
<Button onPress={gallaryButtonPressed} title='Camera / Gallery Button' color='#841584'/>
<Button onPress={addButton} title='Add button' color='#841584'/>
</View>
);
}
}
function gallaryButtonPressed() {
}
function addButton() {
}
export default Add;
所以。例如,我如何访问 Home.js 中的函数 rerenderComponent(),
当我单击 Add.js 中的 “添加按钮” 按钮时 当前链接到函数 addButton() 但 nvm ,
非常感谢:3
【问题讨论】:
-
以下解决方案对您有用吗?
标签: javascript react-native-router-flux