[英]cannot access functions of an associated class in react native
I don't understand, why my code does not work.我不明白,为什么我的代码不起作用。 The Board has a Rack.董事会有一个机架。 Within the Board I want to access a function of the Rack.在板上,我想访问机架的 function。 I get the error this.rack.getFields() is not a function
.我收到错误this.rack.getFields() is not a function
。
Could it be that I cannot use JavaScript like I am used to in Java?难道我不能像以前在 Java 中那样使用 JavaScript 吗?
class Rack extends Component {
...
getFields() {
return this.fields;
}
...
}
class Board extends Component {
constructor(props) {
super(props)
this.fields = this.buildFields();
this.figures = this.buildFigures();
this.rack = this.buildRack();
}
someFunction() {
...
rackFields = this.rack.getFields(); // <--- ???
...
}
}
Board.js The Board has Fields, and a "Rack" itself also containing Fields. Board.js Board 具有字段,并且“机架”本身也包含字段。 Perhaps the problem is caused by the way of buildRack()?也许问题是由 buildRack() 的方式引起的?
import React, { Component } from 'react';
import { ScrollView, View, Text, Button} from 'react-native';
import Draggable from 'react-native-draggable';
import Field from '../Components/Field';
import Figure from '../Components/Figure';
import Rack from '../Components/Rack';
import { Dimensions, DeviceInfo, Platform } from 'react-native';
class Board extends Component {
screenWidth = Math.round(Dimensions.get('window').width);
boardWidth=5;
renderSize=this.screenWidth / this.props.boardWidth;
margin = 1;
fields;
figures;
rack;
constructor(props) {
super(props)
this.state = {
lastRefresh: Date(Date.now()).toString(),
}
this.fields = this.buildFields();
this.figures = this.buildFigures();
this.rack = this.buildRack();
}
headerHeight = 95;
getFieldByPosition(x, y) {
ix = Math.round( (x+this.renderSize/2) / (this.renderSize+this.margin), 0 )-1;
iy = Math.round(((y+this.renderSize/2)-this.headerHeight) / (this.renderSize+this.margin), 0 )-1;
result = [ix, iy];
console.log('getFieldByPosition -> ' + result);
return result;
}
getFieldPosition(ix, iy) {
return [ix * (this.renderSize+this.margin), iy * (this.renderSize+this.margin)];
}
getFieldAt(x, y) {
[ix, iy] = this.getFieldByPosition(x,y);
fieldname = ix+'/'+iy;
if (iy > this.props.boardWidth-1) {
// außerhalb des Boards, im Rack?
fieldName = 'rack_' + ix;
}
// Board Felder abgleichen
for (var i = 0; i < this.fields.length; i++) {
field = this.fields[i];
console.log(field.props.name + " === " + fieldname);
if (field.props.name === fieldname)
return field;
}
// Rack Felder abgleichen
rackFields = this.rack.getFields();
for (var i = 0; i < rackFields.length; i++) {
field = rackFields[i];
console.log(field.props.name + " === " + fieldname);
if (field.props.name === fieldname)
return field;
}
throw 'field at position not found ' + fieldname
}
handleDrag(e, gestureState, draggable) {
console.log('board drag');
this.setState({ color: 'red' })
}
buildFields() {
var fields = [];
for (i = 0; i < this.props.boardWidth; i++) {
for (k = 0; k < this.props.boardWidth; k++) {
fields.push(
<Field name={k+"/"+i}
key={k+"/"+i}
x={(this.renderSize+this.margin)*k}
y={(this.renderSize+this.margin) * i}
renderSize={this.renderSize}
color='green'
board={this}
/>
)
}
}
return fields;
}
buildFigures() {
var figures = [];
figures.push(
<Figure name='K'
key='K'
x={(this.renderSize+this.margin)*2}
y={(this.renderSize+this.margin) * 2}
renderSize={this.renderSize}
color="red"
board={this}
/>
)
return figures;
}
buildRack() {
var rack = (
<Rack rackWidth={5} x={0} y={(this.renderSize+this.margin) * this.props.boardWidth}/>
)
return rack;
}
refreshScreen() {
this.setState({ lastRefresh: Date(Date.now()).toString() })
}
calculateHeight = (this.renderSize+this.margin)*(this.props.boardWidth+2);
render() {
return (
<ScrollView
nestedScrollEnabled
bounces={false}
// You will need to figure out the height of inner content yourself
contentContainerStyle={{ height: this.calculateHeight }}
onScroll={(e)=>handleOnScroll(e)}
>
{ this.fields }
{ this.rack }
{ this.figures }
</ScrollView>
)
}
}
export default Board;
You can't access component's method just like traditional OOP.不能像传统的 OOP 那样访问组件的方法。 Because Rack
is class component .因为Rack
是class 组件。 For that you can make Racks as a normal class (remove extends) or else if there is parent child relation you can pass method using props .为此,您可以将 Racks 制作为普通的 class (删除扩展),否则如果存在父子关系,您可以使用props传递方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.