0
저는 MobX를 처음 사용 했으므로 여기에서 추천했습니다. 다른 파일로 전달하려고하는 간단한 배열이 있습니다. 많은 것을 시도했지만 this.props.store 형태로 배열을받을 수 없습니다. mobx의 부적절한 사용에 대해 밝히면 큰 도움이 될 것입니다. 고맙습니다.관찰 가능에서 관찰자까지 간단한 배열을 전달하는 MobX
import React, { Component } from 'react';
import {
Alert,
AppRegistry,
StyleSheet,
Text,
TouchableHighlight,
View
} from 'react-native';
import styles from '../styles/styles.js';
import {observable, extendObservable} from 'mobx';
//unsure why below code wont work, when trying to export it throws error: object is not a constructor (evaluating 'new rNumStore()')
// const rNumStore = observable([1,2,3]);
// a couple of other options both i am unsure why they do not work
// class rNumStore {
// extendObservable(this, {
// rNumbers: [1,2,3]});
// }
// class rNumStore {
// observable({rNumbers: [1,2,3]});
// }
//Current best working solution below
var rNumStore = function(){
extendObservable(this, {
rNumbers: [1,2,3]
});
}
var Button = React.createClass({
rNumberGen: function(){
store.rNumbers = [Math.random(), Math.random(), Math.random()];
var a = store.rNumbers[0].toString();
var d =store.rNumbers.toString();
Alert.alert(a, d);
//this alert gives mobxArray of random numbers
},
render: function(){
return(
<TouchableHighlight onPress={this.rNumberGen} style= {styles.center}>
<Text style={styles.button}>Generate!</Text>
</TouchableHighlight>
);
}
});
var store = new rNumStore;
export default store;
export {Button};
그런 다음 두 번째 파일은 배열
import React, { Component } from 'react';
import {
Alert,
AppRegistry,
StyleSheet,
TouchableHighlight,
Text,
View
} from 'react-native';
import{
genre1,
genre2,
genre3
} from './genres.js';
import styles from '../styles/styles.js';
import {observer} from 'mobx-react/native';
var Genre = observer(React.createClass({
alert: function(){
//below does not cause an error.
var a = this.props.store;
//undefined ins not an object(evaluating 'this.props.store.rNumbers')
// have tried making 'a' a state : this.props.store.rNumbers same error occurs at that point
var b = this.props.store.rNumbers[1];
var c = b.toString();
Alert.alert('this is', c);
},
render: function(){
let genre = this.props.selected;
return(
<TouchableHighlight onPress={this.alert}>
<View style={styles.genre}>
<Text style={styles.center}>{genre}</Text>
</View>
</TouchableHighlight>
);
},
}));
module.exports = Genre;
메인 앱 코드는 어디에 있습니까? 그 상점을 만들어야한다. 그러면 저장소에 상점을 속성으로 전달한 및
정직하게 Peter, 너는 내 보호자이다. 잠시 후 이것으로 내 해결책을 얻었습니다. 답변으로 올리시겠습니까? 올바른 것으로 표시 할 수 있습니다. 또는 유사한 문제가있는 경우를 대비하여 전체 솔루션을 게시 할 수 있습니다. –
답변으로 게시 됨. 편집 권한이있는 경우 원하는 경우 코드를 추가 할 수 있습니다. –