reactjs - 如何在两个组件的原生反应中导入和使用函数?
问题描述
当我将代码放在一个文件中时,一切正常。但是当我将组件拆分为两个单独的文件时出现错误
"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
})
}
---A组件---
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>
);
}
}
---兄弟对兄弟----
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>
);
}
}
我看不出我做错了什么。你能帮我么?
解决方案
推荐阅读
- python - 从文本中提取文件路径
- angular - 角8,茉莉花
: 找不到对象来监视 bind() - sql-server - SSRS - Executing report when parameter is NULL
- python - Python面向对象编程基本登录系统返回TypeError
- angular - Angular中的两种方式数组绑定
- javascript - 我们什么时候需要在javascript中调用防止默认值?
- c# - “给定的键‘utf8mb4’不在字典中
- android - Android: How to crop an image using Picasso?
- artificial-intelligence - 如何用两个空白检查 n-puzzle 是可解的?
- powershell - 如何同时执行多个命令