如何在React Native中导入和使用函数以实现两个组件?

时间:2019-01-30 09:23:05

标签: reactjs react-native react-router

当我将代码放在一个文件中时,一切正常。但是,当我将组件拆分为两个单独的文件时,出现了错误

"Error: 'updateText' is read only".

我的代码在一个文件中:

import React from 'react';
import {
  Text,
  View,
  TextInput,
  Button
} from 'react-native';

function updateText(TextViewA) {
  this.setState({ TextViewA })
}

class AComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      TextViewA: 'This is A Component'
    };
    updateText = updateText.bind(this);
  }
  render() {
    return(
      <View>
        <Text>{this.state.TextViewA}</Text>
        <TextInput
          onChangeText={(text) => this.setState({TextViewA: text})}
          value={this.state.TextViewA}
        />
        <Button
          title="Click A"
          onPress={() => {this.props.changeTextMainT()}}
        />
      </View>
    );
  }

}

class BComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state={TextViewB: 'This is B Component'}
  }
  render() {
    return (
    <View>
      <Text>{this.state.TextViewB}</Text>
      <TextInput
        onChangeText={(text) => {
        this.setState({TextViewB: text});
        updateText(text)}
        }
        value={this.state.TextViewB}
      />
      </View>
    )
  }
}

export default class SiblingToSibling extends React.Component {
  constructor() {
    super();
    this.state = {
      textMain: 'Init'
    };

    this.changeTextMain = this.changeTextMain;
  }

  changeTextMain = () => {
    this.setState({
      textMain: "Success"
    })
  }
  render() {
    return (
      <View>
        <AComponent
          changeTextMainT={this.changeTextMain}
        />
        <BComponent />
        <Text>{this.state.textMain}</Text>
        <TextInput
          onChangeText={(text) => {
            this.setState({
              textMain: text
            })
          }}
        />
      </View>
    );
  }
}

我像这样拆分组件时的代码:
--- UpdateText ---

export default function updateText(TextViewA) {
  this.setState({
    TextViewA
  })
}

--- AComponent ---

class AComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      TextViewA: 'This is A Component'
    };
    updateText = updateText.bind(this);
  }
  render() {
    return ( <
      View >
      <
      Text > {
        this.state.TextViewA
      } < /Text> <
      TextInput onChangeText = {
        (text) => this.setState({
          TextViewA: text
        })
      }
      value = {
        this.state.TextViewA
      }
      /> <
      Button title = "Click A"
      onPress = {
        () => {
          this.props.changeTextMainT()
        }
      }
      /> <
      /View>
    );
  }

}

--- SiblingToSibling ----

import updateText from './updateText';
    
    class BComponent extends React.Component{
      constructor(props){
        super(props);
        this.state={
          TextViewB: 'This is B Component'
        }
      }
      render(){
        return (
          <View>
            <Text>{this.state.TextViewB}</Text>
            <TextInput 
              onChangeText={(text)=> {this.setState({TextViewB:text});updateText(text)}}
              value={this.state.TextViewB}/>
          </View>
        )
      }
    }
    
    export default class SiblingToSibling extends React.Component{
      constructor(){
        super();
        this.state = {
          textMain:'Init'
        };      
        this.changeTextMain= this.changeTextMain;
      }
    
      changeTextMain = ()=>{
        this.setState({textMain:"Success"})
      }
      render(){
        return(
          <View>        
            <AComponent changeTextMainT={this.changeTextMain}/>
            <BComponent />
            <Text>{this.state.textMain}</Text>
            <TextInput
              onChangeText = {(text)=> {this.setState({textMain:text})}}
            />
          </View>
        );
      }
    }

我看不到我在做什么错。你能帮我吗?

0 个答案:

没有答案