Reputation: 978
Maybe I am getting this error because the props are passed is a string and toFixed()
cannot work on it but I am trying to pass number and for that purpose, I have also added + in after price
like this +pass
to convert into a number but still I am getting this error.
ProductItem.js:
const ProductItem = (props) => {
return (
<View style={styles.product}>
<View style={styles.Touchable}>
<TouchableNativeFeedback onPress={props.onSelect} useForeground>
<View>
<View style={styles.imageContainer}>
<Image style={styles.image} source={{ uri: props.image }} />
</View>
<View style={styles.details}>
<Text style={styles.title}>{props.title}</Text>
<Text style={styles.price}>
{props.price.toFixed(2)} //Here I am getting this error
</Text>
</View>
<View style={styles.actions}>{props.children}</View>
</View>
</TouchableNativeFeedback>
</View>
</View>
);
};
In the above file, I am getting this error.
EditUserProduct.js :
import React, { useState, useCallback, useEffect } from "react";
import {
View,
ScrollView,
Text,
TextInput,
Platform,
StyleSheet,
} from "react-native";
import { useSelector, useDispatch } from "react-redux";
import { HeaderButtons, Item } from "react-navigation-header-buttons";
import CustomHeaderButton from "../../components/UI/HeaderButton";
import * as productActions from "../../store/actions/products";
const EditProductScreen = (props) => {
const dispatch = useDispatch();
const prodId = props.navigation.getParam("productId");
const editedProduct = useSelector((state) =>
state.products.userProducts.find((prod) => prod.id === prodId)
);
console.log(editedProduct);
const [title, setTitle] = useState(editedProduct ? editedProduct.title : "");
const [imageUrl, setImageUrl] = useState(
editedProduct ? editedProduct.imageUrl : ""
);
const [price, setPrice] = useState("");
const [description, setDescription] = useState(
editedProduct ? editedProduct.description : ""
);
const submitHandler = useCallback(() => {
if (editedProduct) {
dispatch(
productActions.updateProduct(prodId, title, description, imageUrl)
);
} else {
dispatch(
productActions.createProduct(
prodId,
title,
description,
imageUrl,
+price
)
);
}
}, [dispatch, prodId, title, description, imageUrl, price]);
useEffect(() => {
props.navigation.setParams({ submit: submitHandler });
}, [submitHandler]);
return (
<ScrollView>
<View style={styles.form}>
<View style={styles.formControl}>
<Text style={styles.label}>Title</Text>
<TextInput
style={styles.input}
value={title}
onChangeText={(text) => setTitle(text)}
/>
</View>
<View style={styles.formControl}>
<Text style={styles.label}>Image URL</Text>
<TextInput
style={styles.input}
value={imageUrl}
onChangeText={(text) => setImageUrl(text)}
/>
</View>
{editedProduct ? null : (
<View style={styles.formControl}>
<Text style={styles.label}>PRICE</Text>
<TextInput
style={styles.input}
value={price}
onChangeText={(text) => setPrice(text)}
/>
</View>
)}
<View style={styles.formControl}>
<Text style={styles.label}>Description</Text>
<TextInput
style={styles.input}
value={description}
onChangeText={(text) => setDescription(text)}
/>
</View>
</View>
</ScrollView>
);
};
EditProductScreen.navigationOptions = (navigationData) => {
const submitFN = navigationData.navigation.getParam("submit");
return {
headerTitle: navigationData.navigation.getParam("productId")
? "Edit Product"
: "Add Product",
headerRight: () => (
<HeaderButtons HeaderButtonComponent={CustomHeaderButton}>
<Item
title="Save"
iconName={
Platform.OS === "android" ? "md-checkmark" : "ios-checkmark"
}
onPress={submitFN}
/>
</HeaderButtons>
),
};
};
In the above file I have added +price
to convert it into a number but still getting this error.
I have also tried parseFloat(props.price.toFixed(2))
then I am getting NaN
.
How can I solve this error?
Upvotes: 0
Views: 1032