如何从我的静态数据中解析和电话号码并将其渲染为可单击的链接项目
我正在使用Expo构建我的第一个React Native应用程序。我的应用程序中有一个常见问题解答部分,其中我用手风琴组件呈现问题和答案。手风琴组件正在用预定义的静态数据集填充。当用户选择一个问题时,答案将在其下方扩展,并在选择另一个问题时崩溃。该手风琴运作良好,但是,在给定的一些答案中,有建议的链接要遵循。
现在,这些链接作为静态字符串呈现。我正在尝试使它们作为可单击的链接呈现。
这是我的代码的一个示例:
ACTORCION组件
import React, { useState } from 'react';
import { StyleSheet,Text,View,TouchableOpacity} from 'react-native';
import * as Animatable from 'react-native-animatable';
import Accordion from 'react-native-collapsible/Accordion';
import data from './DataSet.js
const AccordionList = ({data}) => {
// Default active selector
const [activeSections, setActiveSections] = useState([]);
const setSections = (sections) => {
//setting up a active section state
setActiveSections(sections.includes(undefined) ? [] : sections);
};
const renderHeader = (section, _, isActive) => {
//Accordion Header view - Questions
return (
<Animatable.View
duration={400}
style={[styles.header, isActive ? styles.active : styles.inactive]}
transition="backgroundColor">
<Text style={styles.headerText}>
{section.title}{/* <---Renders Question */}
</Text>
</Animatable.View>
);
};
const renderContent = (section, _, isActive) => {
//Accordion Content view - Answers
return (
<Animatable.View
duration={400}
style={[styles.content, isActive ? styles.active : styles.inactive]}
transition="backgroundColor">
<Animatable.Text
animation={isActive ? 'bounceIn' : undefined}
style={{ textAlign: 'left' }}>
{section.content} {/* <---Renders Answer */}
</Animatable.Text>
</Animatable.View>
);
};
return (
<View>
<Accordion
activeSections={activeSections}
sections={data}
touchableComponent={TouchableOpacity}
renderHeader={renderHeader}
renderContent={renderContent}
duration={400}
onChange={setSections}
/>
</View>
);
};
export default AccordionList;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "black",
},
header: {
backgroundColor: '#F5FCFF',
padding: 15,
marginTop:10,
borderRadius: 5,
marginHorizontal:10,
},
headerText: {
textAlign: 'center',
fontSize: 16,
fontWeight: '700',
},
content: {
padding: 20,
backgroundColor: "grey",
borderRadius: 5,
marginHorizontal:10,
textAlign:'right',
lineHeight:1.5
},
inactive: {
backgroundColor: 'rgba(255,255,255,1)',
},
active: {
backgroundColor: 'rgba(245,252,255,1)',
},
selectTitle: {
fontSize: 20,
fontWeight: '500',
padding: 10,
textAlign: 'center',
color:"#efefef"
},
});
dataset.js.js
data:[
{
title:"What is Ameelio?",
content:"Ameelio is a nonprofit service that allows friends and families to send letters, postcards, games and more for free through their mobile application."
},
{
title:"What can I do on Ameelio?",
content:"Yes. You may call customer service any time at 1 (800) 555-5555"
},
{
title:"Where can I find more information?",
content:"You can find more information on https://ameelio.org."
},
{
title:"How do I get the Ameelio app?",
content:"Navigate to https://play.google.com/store/apps/details?id=com.ameelio.letters_mobile to download the free app."
},
]
在摘要中,我正在尝试找出如何从我的静态数据中解析和电话号码,并将其作为可单击的链接项目渲染。
I am building my very first React Native app using Expo. I have a FAQs section in my app where I am rendering questions and answers with an Accordion component. The Accordion component is being populated with a predefined static data set. When the user selects a question, the answer expands below it, and collapses when another question is selected. The Accordion is working well, however, in some of the given answers there are suggested links to follow.
Right now these links are rendering as static strings. I am trying to get them to render as clickable links.
Here is an example of my code:
Accordion Component
import React, { useState } from 'react';
import { StyleSheet,Text,View,TouchableOpacity} from 'react-native';
import * as Animatable from 'react-native-animatable';
import Accordion from 'react-native-collapsible/Accordion';
import data from './DataSet.js
const AccordionList = ({data}) => {
// Default active selector
const [activeSections, setActiveSections] = useState([]);
const setSections = (sections) => {
//setting up a active section state
setActiveSections(sections.includes(undefined) ? [] : sections);
};
const renderHeader = (section, _, isActive) => {
//Accordion Header view - Questions
return (
<Animatable.View
duration={400}
style={[styles.header, isActive ? styles.active : styles.inactive]}
transition="backgroundColor">
<Text style={styles.headerText}>
{section.title}{/* <---Renders Question */}
</Text>
</Animatable.View>
);
};
const renderContent = (section, _, isActive) => {
//Accordion Content view - Answers
return (
<Animatable.View
duration={400}
style={[styles.content, isActive ? styles.active : styles.inactive]}
transition="backgroundColor">
<Animatable.Text
animation={isActive ? 'bounceIn' : undefined}
style={{ textAlign: 'left' }}>
{section.content} {/* <---Renders Answer */}
</Animatable.Text>
</Animatable.View>
);
};
return (
<View>
<Accordion
activeSections={activeSections}
sections={data}
touchableComponent={TouchableOpacity}
renderHeader={renderHeader}
renderContent={renderContent}
duration={400}
onChange={setSections}
/>
</View>
);
};
export default AccordionList;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "black",
},
header: {
backgroundColor: '#F5FCFF',
padding: 15,
marginTop:10,
borderRadius: 5,
marginHorizontal:10,
},
headerText: {
textAlign: 'center',
fontSize: 16,
fontWeight: '700',
},
content: {
padding: 20,
backgroundColor: "grey",
borderRadius: 5,
marginHorizontal:10,
textAlign:'right',
lineHeight:1.5
},
inactive: {
backgroundColor: 'rgba(255,255,255,1)',
},
active: {
backgroundColor: 'rgba(245,252,255,1)',
},
selectTitle: {
fontSize: 20,
fontWeight: '500',
padding: 10,
textAlign: 'center',
color:"#efefef"
},
});
DataSet.js
data:[
{
title:"What is Ameelio?",
content:"Ameelio is a nonprofit service that allows friends and families to send letters, postcards, games and more for free through their mobile application."
},
{
title:"What can I do on Ameelio?",
content:"Yes. You may call customer service any time at 1 (800) 555-5555"
},
{
title:"Where can I find more information?",
content:"You can find more information on https://ameelio.org."
},
{
title:"How do I get the Ameelio app?",
content:"Navigate to https://play.google.com/store/apps/details?id=com.ameelio.letters_mobile to download the free app."
},
]
In summary, I am trying to figure out how to parse URLs and phone numbers out of my static data and render them as clickable Linking items.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过使用
react-native-hyperlink
解决了此问题。I solved this issue by using
react-native-hyperlink
.