在组件类中调用函数

时间:2018-03-21 00:57:15

标签: javascript reactjs ecmascript-6

我有以下变量Panel,它已被分配了一组组件。此变量已在const registerForm = [ <div key={0} style={styles.form.inlineContainer}> ... <TextField floatingLabelText="First Name" hintText="First Name" floatingLabelFixed={true} style={styles.form.inlineContainer.spacing} underlineFocusStyle={styles.form.field.borderColor} floatingLabelFocusStyle={styles.form.field.color} floatingLabelStyle={styles.form.field.fontSize} onChange={(input) => this.handleInput(input)} /> ... </div> ]; 类之外定义。

handleInput

然后我有一个具有函数_this.handleInput is not a function的类。在文本字段中输入内容时,会产生以下错误:export default class Panel extends React.Component { constructor() { super(); this.state = { panel: 0 } } handlePanelChange = (panel) => { this.setState({ panel: panel }); }; handleInput = (input) => { console.log(input); }; render() { const panel = this.state.panel; const form = (panel === 0) ? signInForm : registerForm; return ( {form} ); } }

onChange

如果可能,我如何才能将handleInput事件调用//main program LinkedListHT lst1 = new LinkedListHT(); System.out.println("lst1 size is " + lst1.size()); lst1.addHead(10); System.out.println("lst1 size is " + lst1.size()); System.out.println(lst1); lst1.addHead(20); lst1.addHead(20); lst1.addHead(20); lst1.addHead(20); lst1.addHead(50); lst1.addHead(431); lst1.addHead(57); System.out.println(lst1); lst1.addTail(60); lst1.addTail(70); lst1.addTail(20); System.out.println(lst1); System.out.println("20 is in there " + lst1.count(20)); System.out.println("lst1 size is " + lst1.size()); System.out.println(lst1); lst1.delHead(); System.out.println(lst1); lst1.delAll(20); System.out.println(lst1); System.out.println(); System.out.println(); System.out.println(); System.out.println("Normal sum program: " + lst1.sum()); System.out.println(lst1.sumEven()); System.out.println(); System.out.println(); System.out.println(); LinkedListHT lst2 = new LinkedListHT(); lst2.addHead(19); lst2.addHead(45); lst2.addHead(12); lst2.addHead(64); lst1.addTail2(lst2); System.out.println(lst1); //linkedlist class private Node head = null; private Node tail = null; public void addHead(int x){ //insert x at head of list Node nw = new Node(x); if(head == null){ head = nw; tail = nw; } else{ nw.setNext(head); head = nw; } } public void addTail(int x){ //add at head Node nw = new Node(x); if(head == null){ head = nw; tail = nw; } else{ tail.setNext(nw); tail = nw; } public void addTail2(LinkedListHT lst){ Node k = lst.head; while (k != null) { System.out.println("Now adding " + k.data()); this.addTail(k.data()); System.out.println(this); k = k.next() public void delTail(){ if(head == tail){ head = null; tail = null; } else{ Node k = head; while(k.next() != tail) k = k.next(); k.setNext(null); tail = k; } } public int size(){ Node k = head; int len = 0; while(k != null){ len++; k = k.next(); } return len; } public void delHead(){ if(head != null) head = head.next(); } public void delAll(int x){ Node k = head; Node bk = head; boolean found = false; while(k != null) { if(k.data() == x) { found = true; } else{ bk = k; k = k.next();} if(found) { //System.out.println("found one!"); if(k == head) head = k.next(); else { bk.setNext(k.next()); k = k.next(); } } //System.out.println("first, k is now " + k.data() + " and bk is now " + bk.data()); //k = k.next(); //System.out.println("now, k is now " + k.data() + " and bk is now " + bk.data()); found = false; } } public int sum(){ Node k = head; int a = 0; while(k != null){ a += k.data(); k = k.next(); } return a; } public int sumEven(){ Node k = head; int a = 0; while(k != null){ if(k.data() % 2 == 0) { a += k.data(); } k = k.next(); } return a; } public int count(int x){ Node k = head; int c = 0; while(k != null){ if(k.data() == x) c++; k = k.next(); } return c; } public String toString(){ if(head == null) return "[]"; String s = "["; Node k = head; while(k.next() != null){ s = s + k.data()+", "; k = k.next(); } s = s + k.data()+"]"; return s; } //Node Class private class Node{ int data; Node next; public Node(int x){ data = x; next = null; } public Node next(){return next;} public void setNext(Node p){ next = p; } public void set(int x){data = x;} public int data(){return data;} } //output Now adding 64 [431, 50, 10, 60, 70] Now adding 12 [431, 50, 10, 60, 70] Now adding 45 [431, 50, 10, 60, 70] Now adding 19 [431, 50, 10, 60, 70]

1 个答案:

答案 0 :(得分:0)

由于它是一个array,它将呈现一个UI元素,那么为此创建一个组件呢?然后,您可以通过props将您的方法传递给React tutorial指出:

class Square extends React.Component {
render() {
    return (
      <button className="square">
        {this.props.value}
      </button>
    );
  }
}

class Board extends React.Component {
  renderSquare(i) {
    return <Square value={i} />;
}
相关问题