I am created a simple box component which takes image source as props. But while calling HomeScreenBox the image is not rendering..
<HomeScreenBox
BoxName="Add Marks"
BoxImage='require("../assets/images/person.jpg")'
/>
const HomeScreenBox = (props) => {
return (
<View>
<Image source={props.BoxImage} />
<Text>{props.BoxName}</Text>
</View>
);
};
require should not be a string, try
BoxImage={require("../assets/images/person.jpg")}
Related
I would like to implement a custom component that accepts a nested component. Something that can be used like this.
<MyCustomComponent>
<AnyNestedComponent/>
</MyCustomComponent>
I searched about this but only found the use of this.props, which is not what I expect.
How do I implement MyCustomComponent in React Native version 0.68?
Note: MyCustomComponent will consist of View(s).
Its fairly simple in RN,
Your customComponent should be like =
const CumstomComp = ({props = {}, children = null}) => {
return(
<View style={{backgroundColor:"red"}} >
{children}
</View>
)
}
and then you use it like this
App.js or whatever file
const App = () => {
return(
<View>
<CustomComp>
<Flatlist />
<View />
</CustomComp>
</View>
)
}
Hope it helps. feel free for doubts
Im trying to pass a function handleNewFavourite (which updates my favouriteList state array) from my HomeScreen to my DetailsScreen via navigation params but Im getting the following error: Non-serializable values were found in the navigation state
How should I pass functions that modified the state between different stack screens?
HomeScreen code:
<FlatList
data={checkCategory()}
renderItem={({item}) => (
<TouchableOpacity
onPress={() =>
navigation.navigate('Details', {
item,
handleNewFavourite,
})
}>
<LessonCard lesson={item} />
</TouchableOpacity>
)}
/>
DetailScreen code:
const LessonDetails = ({lesson, handleNewFavourite}: LessonProps) => {
const [favourite, setFavourite] = useState<boolean>(lesson.favourite);
return (
<LessonDetailsContainer>
<LessonDetailsInfoContainer>
<LessonDetailsCategoryHead>
<LessonDetailsCategory>{lesson.category}</LessonDetailsCategory>
<TouchableOpacity
onPress={() => {
setFavourite(!favourite);
handleNewFavourite(lesson);
}}>
<LessonDetailsFavouriteIcon>
{favourite ? '❤️' : '🤍'}
</LessonDetailsFavouriteIcon>
</TouchableOpacity>
</LessonDetailsCategoryHead>
<LessonDetailsTitle>{lesson.title}</LessonDetailsTitle>
<LessonDetailsAuthor>{lesson?.author}</LessonDetailsAuthor>
</LessonDetailsInfoContainer>
<LessonDetailsCardImage
source={{
uri: lesson.image,
}}
/>
<LessonDetailsContentContainer>
<LessonDetailsDescriptionText>
{lesson.content}
</LessonDetailsDescriptionText>
</LessonDetailsContentContainer>
</LessonDetailsContainer>
);
};
export default LessonDetails;
For situation like this, you should learn global state management. ( Context API - Redux etc. )
I think you are disrupting in the wrong way the parameters passed to DetailScreen it should be something like this:
const LessonDetails = ({route}: LessonProps) => {
const {lesson, handleNewFavourite} = route.params;
// The rest of your component here
}
As the documentation here suggested. But as #omerfarukose mentioned is not a bad idea to use state management in this particular scenario
Given the following simple component example which uses styled components for the Pressable:
const StyledPressable = styled.Pressable = props => props.buttonStyle
const Icon: FC<{buttonStyle: string}> = ({buttonStyle}) => {
return (
<StyledPressable buttonStyle={buttonStyle} >
<SomeIconComponent/>
</StyledPressable>
)
}
I would like to create a reusable component which takes in style props while still using styled components. Normally using the vanilla react native styles, this is really simple one would just pass in the style object as a prop an use it as a value in the necessary component. How would i achieve this behavior using styled components? my initial guess is that the buttonStyle has to be a string in the same format as styled components, but how would i make it so that the style which is declared inside the '' of lets say StyledPressable in this example is equal to the passed buttonStyle prop?
Since i am learning styled components, i am very open to any other way to achieve this behavior while using styled components.
import styled from "styled-components/native"
const Pressable = styled.Pressable(props => (props.buttonStyle))
export default function App() {
return (
<View style={styles.container}>
<Pressable buttonStyle={{ backgroundColor: "pink" }}>
<Text>This is button</Text>
</Pressable>
</View>
);
}
Inder's answer using some Typescript
import styled from "styled-components/native"
interface PressableProps {
buttonStyle?: CSSObject
}
const Pressable = styled.Pressable<PressableProps>({ buttonStyle }) => buttonStyle)
export default function App() {
return (
<View style={styles.container}>
<Pressable buttonStyle={{ backgroundColor: "pink" }}>
<Text>This is button</Text>
</Pressable>
</View>
);
}
I have dyna;ic number of items in an array, and i should create text inputs to chnage these items, so how can i do it please ? any ideas ? I don't know how to use flatlist with text inouts.and how to handle every input, because the number is dynamic I'm new to react.
You can create a component as item to render in the flatlist, and in the ItemComponent you can put anything,
Eg:
const ItemCard = ({...props}) => {
return (
<View>
<Image />
<TextInput />
</View>
)
}
Other component:
<FlatList renderItem={({item, index}) => <ItemCard />} />
nameofArray.map(
(arrayObject)=>
{
<TextInput
value={arrayObject.value}//here comes which value you want to disp
/>
}
)
If your data is large then you should consider flatlist
I am building a react native application and am trying to set the source of an image based on the contents that I am passing to the component as a prop. My code is as follows:
const nameOfComponent = props => {
return (
<View style={some style}>
<Image source={require(props.imageURL)} style={some style} />
</View>
)
}
The prop imageURL is being passed down from the parent component by:
<nameOfComponent imageURL="../dir/name.png" />
When including the code above in my project, I am met with an error saying "Invalid call at line 15: require(props.imageURL)".
Why is this happening? Thanks!
According to React-Native, all your image sources need to be loaded before compiling your bundle. So change your code as,
const nameOfComponent = props => (
<View style={{ some_style }}>
<Image source={props.imageURL} style={{ some_style }} />
</View >
)
Call your child component as,
<nameOfComponent imageURL={require('imagepath')} />
Hope this helps you. Feel free for doubts.