我正在使用一个没有标题的反应本机天才聊天示例。我试图从native-base添加标头。在集成之后,它说错误的是在它已经存在的情况下缺少封闭标记。怎么能这个固定?这是代码:
import React from 'react';
import {
Platform,
StyleSheet,
Text,
View,
} from 'react-native';
import { Container, Header, Left, Body, Right, Button, Icon, Title, Text } from 'native-base';
import {GiftedChat, Actions, Bubble, SystemMessage} from 'react-native-gifted-chat';
import CustomActions from './CustomActions';
import CustomView from './CustomView';
export default class Example extends React.Component {
constructor(props) {
super(props);
this.state = {
messages: [],
loadEarlier: true,
typingText: null,
isLoadingEarlier: false,
};
this._isMounted = false;
this.onSend = this.onSend.bind(this);
this.onReceive = this.onReceive.bind(this);
this.renderCustomActions = this.renderCustomActions.bind(this);
this.renderBubble = this.renderBubble.bind(this);
this.renderSystemMessage = this.renderSystemMessage.bind(this);
this.renderFooter = this.renderFooter.bind(this);
this.onLoadEarlier = this.onLoadEarlier.bind(this);
this._isAlright = null;
}
componentWillMount() {
this._isMounted = true;
this.setState(() => {
return {
messages: require('./data/messages.js'),
};
});
}
componentWillUnmount() {
this._isMounted = false;
}
onLoadEarlier() {
this.setState((previousState) => {
return {
isLoadingEarlier: true,
};
});
setTimeout(() => {
if (this._isMounted === true) {
this.setState((previousState) => {
return {
messages: GiftedChat.prepend(previousState.messages, require('./data/old_messages.js')),
loadEarlier: false,
isLoadingEarlier: false,
};
});
}
}, 1000); // simulating network
}
onSend(messages = []) {
this.setState((previousState) => {
return {
messages: GiftedChat.append(previousState.messages, messages),
};
});
// for demo purpose
this.answerDemo(messages);
}
answerDemo(messages) {
if (messages.length > 0) {
if ((messages[0].image || messages[0].location) || !this._isAlright) {
this.setState((previousState) => {
return {
typingText: 'React Native is typing'
};
});
}
}
setTimeout(() => {
if (this._isMounted === true) {
if (messages.length > 0) {
if (messages[0].image) {
this.onReceive('Nice picture!');
} else if (messages[0].location) {
this.onReceive('My favorite place');
} else {
if (!this._isAlright) {
this._isAlright = true;
this.onReceive('Alright');
}
}
}
}
this.setState((previousState) => {
return {
typingText: null,
};
});
}, 1000);
}
onReceive(text) {
this.setState((previousState) => {
return {
messages: GiftedChat.append(previousState.messages, {
_id: Math.round(Math.random() * 1000000),
text: text,
createdAt: new Date(),
user: {
_id: 2,
name: 'React Native',
// avatar: 'https://facebook.github.io/react/img/logo_og.png',
},
}),
};
});
}
renderCustomActions(props) {
if (Platform.OS === 'ios') {
return (
<CustomActions
{...props}
/>
);
}
const options = {
'Action 1': (props) => {
alert('option 1');
},
'Action 2': (props) => {
alert('option 2');
},
'Cancel': () => {},
};
return (
<Actions
{...props}
options={options}
/>
);
}
renderBubble(props) {
return (
<Bubble
{...props}
wrapperStyle={{
left: {
backgroundColor: '#f0f0f0',
}
}}
/>
);
}
renderSystemMessage(props) {
return (
<SystemMessage
{...props}
containerStyle={{
marginBottom: 15,
}}
textStyle={{
fontSize: 14,
}}
/>
);
}
renderCustomView(props) {
return (
<CustomView
{...props}
/>
);
}
renderFooter(props) {
if (this.state.typingText) {
return (
<View style={styles.footerContainer}>
<Text style={styles.footerText}>
{this.state.typingText}
</Text>
</View>
);
}
return null;
}
render() {
return (
<Header>
<Left>
<Button transparent>
<Icon name='arrow-back' />
</Button>
</Left>
<Body>
<Title>Header</Title>
</Body>
<Right>
<Button transparent>
<Text>Cancel</Text>
</Button>
</Right>
</Header>
<GiftedChat
messages={this.state.messages}
onSend={this.onSend}
loadEarlier={this.state.loadEarlier}
onLoadEarlier={this.onLoadEarlier}
isLoadingEarlier={this.state.isLoadingEarlier}
user={{
_id: 1, // sent messages should have same user._id
}}
renderActions={this.renderCustomActions}
renderBubble={this.renderBubble}
renderSystemMessage={this.renderSystemMessage}
renderCustomView={this.renderCustomView}
renderFooter={this.renderFooter}
/>
);
}
}
const styles = StyleSheet.create({
footerContainer: {
marginTop: 5,
marginLeft: 10,
marginRight: 10,
marginBottom: 10,
},
footerText: {
fontSize: 14,
color: '#aaa',
},
});
它显示有关此行中缺少的封闭标记的语法错误
<GiftedChat
messages={this.state.messages}
onSend={this.onSend}
loadEarlier={this.state.loadEarlier}
onLoadEarlier={this.onLoadEarlier}
isLoadingEarlier={this.state.isLoadingEarlier}
user={{
_id: 1, // sent messages should have same user._id
}}
renderActions={this.renderCustomActions}
renderBubble={this.renderBubble}
renderSystemMessage={this.renderSystemMessage}
renderCustomView={this.renderCustomView}
renderFooter={this.renderFooter}
/>
最有趣的部分是,在上面的giftedchat组件中添加本机基本标头之前,它没有显示出这样的错误,而在集成之后它说出了奇怪的错误。是什么导致这个?
答案 0 :(得分:0)
您只能返回包含子元素/组件的单个元素/组件。您正在尝试返回2个兄弟组件。用View或类似组件包裹它们然后返回。
示例强>
render() {
return (
<View>
<Header>
<Left>
<Button transparent>
<Icon name='arrow-back' />
</Button>
</Left>
<Body>
<Title>Header</Title>
</Body>
<Right>
<Button transparent>
<Text>Cancel</Text>
</Button>
</Right>
</Header>
<GiftedChat
messages={this.state.messages}
onSend={this.onSend}
loadEarlier={this.state.loadEarlier}
onLoadEarlier={this.onLoadEarlier}
isLoadingEarlier={this.state.isLoadingEarlier}
user={{
_id: 1, // sent messages should have same user._id
}}
renderActions={this.renderCustomActions}
renderBubble={this.renderBubble}
renderSystemMessage={this.renderSystemMessage}
renderCustomView={this.renderCustomView}
renderFooter={this.renderFooter}
/>
</View>
);
}