In your FoodPost React component, the string timeAgo is currently being initialized in the state object inside the constructor. The timeAgo property is set using a function getTimeAgo(props.data). The string timeAgo is then used in the subtitle property of the Card.Title.
However, it looks like you might want to directly initialize timeAgo as a string property rather than derive it from a function during initialization. If that's the aim, you simply need to set timeAgo directly in the constructor.
Here's your updated FoodPost component to initialize timeAgo as a string properly:
import React from 'react';
import { View, Card } from 'react-native-paper';
// Assuming getTimeAgo is a function that calculates the time ago based on the given data
const getTimeAgo = (data) => {
// dummy implementation
return "2 hours ago"; // Replace this with your actual implementation
};
type Props = {
addMessage: Function,
data: any, // replace `any` with the actual type of `data`
theme: object, // replace `object` with the actual type of `theme`
};
class FoodPost extends React.Component<Props> {
constructor(props: Props) {
super(props);
this.state = {
expanded: false,
timeAgo: getTimeAgo(props.data)
};
}
handlePress = () => {
console.log("pressed lol");
this.setState(prevState => ({ expanded: !prevState.expanded }));
}
render() {
const { addMessage, data, theme, ...rest } = this.props;
const { timeAgo } = this.state;
return (
<View style={{ margin: 8 }}>
<Card mode="contained" style={{ backgroundColor: theme.colors.inverseOnSurface }} onPress={this.handlePress}>
<Card.Title
titleStyle={{ fontWeight: 'bold', paddingTop: 8 }}
titleVariant="titleLarge"
titleNumberOfLines={2}
title={data.location}
subtitle={timeAgo}
/>
</Card>
</View>
);
}
}
export default FoodPost;
Key points in the above code:
- Initialization in Constructor: The
timeAgo state is initialized in the constructor using the getTimeAgo function.
- Using
timeAgo in Render: Destructured timeAgo from the state object and used it in the Card.Title's subtitle property.
Make sure you replace the dummy implementation of getTimeAgo with your actual function logic for getting the time ago string based on the provided data.