I want my Paper Style automatically changed when the screen is big or medium or small.
here what i have imported
import React from 'react';
import reactMixin from 'react-mixin';
import ResponsiveMixin from 'react-responsive-mixin';
import Paper from 'material-ui/lib/paper';
i use ResponsiveMixin to determine screen changed
Paperstyle={this.state} where i want to have value changed automatically. and those value come from componentDidMount()
export class MainLayout extends React.Component {
constructor(props) {
super(props);
this.state = {
height: '100%',
paddingTop: 60,
marginLeft: 258,
marginRight: 6,
paddingLeft: '4%',
paddingRight: '4%'
}
}
componentDidMount() {
this.media({maxWidth: 600}, function () {
/*small*/
this.state = {
height: '100%',
paddingTop: 60,
marginLeft: '3%',
marginRight: '3%',
paddingLeft: '2%',
paddingRight: '2%'
}
}.bind(this));
this.media({minWidth: 601, maxWidth: 1024}, function () {
/*medium*/
this.state = {
height: '100%',
paddingTop: 60,
marginLeft: '3%',
marginRight: '3%',
paddingLeft: '6%',
paddingRight: '6%'
}
}.bind(this));
this.media({minWidth: 1025}, function () {
/*large*/
this.state = {
height: '100%',
paddingTop: 60,
marginLeft: 258,
marginRight: 6,
paddingLeft: '4%',
paddingRight: '4%'
}
}.bind(this));
}
render() {
const {header, content, footer} = this.props; // destructure this.props to consts
return (
<div>
<header>
{header}
</header>
<main>
<Paper style={this.state} zDepth={1}>
{content}
</Paper>
</main>
<footer>
{footer}
</footer>
</div>
)
}
}
reactMixin(MainLayout.prototype, ResponsiveMixin);
Your help will greatly appreciated :D Thanks so much!
Why do you store styles in component's state? Try this
The componentDidMount() method runs only one time after the component output has been rendered to the DOM according to the documentation. So event if screen size change, nothing will happen. #Material-UI provide a method called withStyles that helps to wrap styles and provide a classes object that containts all styles you defined. The withStyles method automatically update when screen size change. Here is and exemple:
import React from 'react';
import { withStyles } from '#material-ui/core/styles';
const styles = theme => ({
paper: { // For extra small screens (phones)
height: '100%',
paddingTop: 60,
marginLeft: '3%',
marginRight: '3%',
paddingLeft: '2%',
paddingRight: '2%',
[theme.breakpoints.up('sm')]: { // For small screens (tablets)
paddingLeft: '6%',
paddingRight: '6%'
},
[theme.breakpoints.up('md')]: { // For medium screens and above
marginLeft: 258,
marginRight: 6,
paddingLeft: '4%',
paddingRight: '4%'
}
}
})
export class MainLayout extends React.Component {
render() {
const {header, content, footer, classes} = this.props; // destructure this.props to consts
return (
<div>
<header>
{header}
</header>
<main>
<Paper className={classes.paper} zDepth={1}>
{content}
</Paper>
</main>
<footer>
{footer}
</footer>
</div>
)
}
}
// That injects `classes` object styles in your component
const YourStyledComponent = withStyles(styles)(MainLayout);
export default YourStyledComponent; // Export or do what you want with the component
Related
I am trying to fetch posts from a Wordpress API including image sources and then display them in Snap Carousel, However images are not displaying while the text is displayed and images is getting undefined.
Here is the code, the URL i have removed for now.
Everything seems to be working fine, only the images are not loading.
import React, { Component } from 'react';
import { Text, View, Button, Image, flex } from 'react-native';
import Icon from 'react-native-vector-icons/Feather';
import Carousel from 'react-native-snap-carousel';
class HomeSlider extends Component{
constructor(props){
super(props);
this.state = {
activeIndex:0,
posts: {},
loading: true,
}
}
componentDidMount(){
fetch("https://#/wp-json/wp/v2/posts?per_page=4")
.then((response) => response.json())
.then((data => {
this.setState({
posts: data,
loading: false,
})
}
))
}
renderItem({item,index}){
return (
<View style={{
borderTopLeftRadius: 20,
borderTopRightRadius: 20,
borderRadius: 20,
height: 250,
marginLeft: 16,
backgroundColor: 'white',
elevation: 20,
marginBottom:20,
marginTop: 20,
shadowColor: 'white',
shadowOffset: {
width: 0,
height: 10,
},
shadowOpacity: 0.58,
shadowRadius: 16.00,
display: flex, flexDirection: 'column'}}>
<Image style={{height: 150, width: '100%' ,borderTopLeftRadius: 20,
borderTopRightRadius: 20}}
source={{uri: item.illustration}} />
<Text style={{fontSize: 14, padding: 12 ,marginTop: 20, fontFamily: 'poppins_semibold'}}>{item.title}</Text>
</View>
)
}
render(){
if(this.state.loading){
return(<View><Text>Loading</Text></View>)
}else{
const carouselItems = this.state.posts.map(function(x){
return (
{
title: x.title.rendered,
illustration: x.better_featured_image.source_url,
}
)
})
return(
<View style={{ flex: 1, flexDirection:'row', marginTop: 20, backgroundColor: '#FBFEFB'}}>
<Carousel
loop={true}
loopClonesPerSide={2}
autoplay={true}
autoplayDelay={200}
autoplayInterval={15000}
layout={"default"}
ref={ref => this.carousel = ref}
data={carouselItems}
sliderWidth={100}
itemWidth={350}
renderItem={this.renderItem}
onSnapToItem = { index => this.setState({activeIndex:index}) } />
</View>
)
}
}
}
export default HomeSlider;
Looks perfect!
Just checked in below code whether illustration is giving you the correct URL.
const carouselItems = this.state.posts.map(function(x){
return (
{
title: x.title.rendered,
illustration: x.better_featured_image.source_url, // test here by placing dummy url.
}
)
})
I'm new to react native development and I have a problem hope someone can help me!
I'm trying to show some components in a screen, I'm using native base and when I try to put the HomeHeader component and the MainSection component together in the Home Screen, They show with a huge space between them and if I try to duplicate the components, white space is shown between them also!
I don't know why this happens and I hope if someone can help me
Attached code and screenshots for the result on the genymotion emulator
Thanks in advance!
Home.js
import React from "react";
import { Container, Header, Content } from "native-base";
import { ImageBackground, Image, ScrollView } from "react-native";
import MainSection from "../Components/MainSection";
import HomeHeader from "../Components/HomeHeader";
export default class Home extends React.Component {
static navigationOptions = {
header: null
};
render() {
return (
<Container>
<Content>
<HomeHeader />
<MainSection />
<MainSection />
<MainSection />
</Content>
</Container>
);
}
}
HomeHeader.js
import React from "react";
import { Container, Text } from "native-base";
import { Image, ImageBackground } from "react-native";
export default class HomeHeader extends React.Component {
render() {
return (
<Container>
<ImageBackground
source={require("../Assets/Backgrounds/home-header.png")}
style={{
width: null,
flex: 1,
height: 130,
flexDirection: "row",
justifyContent: "center"
}}
>
</ImageBackground>
</Container>
);
}
}
MainSection.js
import React from "react";
import { Container, Content, Text, Button, View } from "native-base";
import { ImageBackground, Image, ScrollView } from "react-native";
export default class MainSection extends React.Component {
render() {
return (
<Container>
<Content>
<View
style={{
flex: 1,
flexDirection: "row",
marginTop: 13
}}
>
<Image
source={require("../Assets/Vector_Icons/home-first-icon.png")}
style={{ height: 50, width: 50, marginLeft: 25 }}
/>
<Text style={{ marginTop: 15, marginLeft: 10, marginRight: 50 }}>
Places For Going Out
</Text>
<Button transparent success style={{ marginTop: 3 }}>
<Text>View More</Text>
</Button>
</View>
<View
style={{
flex: 1,
flexDirection: "row",
marginTop: 13,
marginLeft: 25
}}
>
<ScrollView
horizontal={true}
showsHorizontalScrollIndicator={false}
>
<Image
source={require("../Assets/Backgrounds/splash-bg.png")}
style={{
width: 150,
height: 150,
borderRadius: 5,
marginRight: 10
}}
/>
<Image
source={require("../Assets/Backgrounds/splash-bg.png")}
style={{
width: 150,
height: 150,
borderRadius: 5,
marginRight: 10
}}
/>
<Image
source={require("../Assets/Backgrounds/splash-bg.png")}
style={{ width: 150, height: 150, borderRadius: 5 }}
/>
</ScrollView>
</View>
</Content>
</Container>
);
}
}
Result
I'm trying to style my renderItem in FlatList but elevation not working properly. Is there anything I'm wrong or this is a React Native issue?
I tested ListView too but it still not working properly
This is TodoItem component
import React, { Component } from 'react'
import { Text, View, StyleSheet } from 'react-native'
const styles = StyleSheet.create({
container: {
height: 60,
backgroundColor: 'white',
borderRadius: 10,
shadowColor: '#000',
shadowOffset: { width: 2, height: 2 },
shadowOpacity: 0.4,
shadowRadius: 2,
elevation: 3,
justifyContent: 'center',
paddingHorizontal: 30,
marginBottom: 12
},
text: {
fontSize: 18,
fontWeight: '400',
color: '#080808'
}
})
export default class TodoItem extends Component {
render() {
return (
<View style={styles.container}>
<Text style={styles.text}> {this.props.text} </Text>
</View>
)
}
}
And this is where I call it in FlatList
<FlatList
data={this.props.items}
renderItem={(item) => <TodoItem key={item.index} text={item.item} />}
/>
The point is that elevation works properly if I don't use FlatList like this
<TodoItem text="Hello world" />
What I excepted
What I see
Most issues like this are caused by styling that is applied to your surrounding view or the row that you are trying to render.
If you add a marginHorizontal: 10 to the styles.container for the row that should probably do it for you.
Here is a simplified example where the edges of the row are not cut off. It has a couple of tweaks to make it work, using state.items instead of props.items and changing the style name for the TodoItem to itemContainer. It should give you an idea of how to implement it.
import * as React from 'react';
import { Text, View, StyleSheet, FlatList } from 'react-native';
import { Constants } from 'expo';
export default class App extends React.Component {
state = {
items: [
'Hello'
]
}
render() {
return (
<View style={styles.container}>
<FlatList
data={this.state.items}
renderItem={(item) => <TodoItem key={item.index} text={item.item} />}
/>
</View>
);
}
}
class TodoItem extends React.Component {
render() {
return (
<View style={styles.itemContainer}>
<Text style={styles.text}> {this.props.text} </Text>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight + 10,
backgroundColor: '#ecf0f1',
},
itemContainer: {
marginHorizontal: 10,
height: 60,
backgroundColor: 'white',
borderRadius: 10,
shadowColor: '#000',
shadowOffset: { width: 2, height: 2 },
shadowOpacity: 0.4,
shadowRadius: 2,
elevation: 3,
justifyContent: 'center',
paddingHorizontal: 30,
marginBottom: 12
},
text: {
fontSize: 18,
fontWeight: '400',
color: '#080808'
}
});
Here is a snack showing it working https://snack.expo.io/#andypandy/flatlist-with-elevation-on-rows
I can't find what's wrong in here: https://hastebin.com/fuwofavuso.scala (I can't post it here, and I didn't want to indent all lines).
import React, { Component} from 'react';
import { Text, View, StyleSheet, Image, TextInput, AppRegistry} from 'react-native';
import { Constants, BlurView } from 'expo';
export default class App extends Component {
render() {
return (
<View style={{ height: 200, width: 300 }}>
<InputText/>
</View>
);
}
}
class InputText extends Component {
constructor(props) {
super(props);
this.state = {
text: 'lel',
};
}
render() {
return (
<div>
<TextInput
onChangeText={(text) => this.setState({ text })}
value={this.state.text}
/>
<Text value={this.state.text}></Text>
</div>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
},
paragraph: {
margin: 24,
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
color: '#34495e',
},
});
AppRegistry.registerComponent('Unknown Snack', () => InputText);
My error is:
undefined is not an object evaluating _reactnative.appregistry.registerComponent()
I am running it on Expo Snack
As you can see here, registerComponent function has two arguments that the second argument is your main component that in your case the name of component seems to be wrong (your problem may be because of this). I think your code should be like this:
AppRegistry.registerComponent('yourAppKey', App);
I am completely new to React and I need to make an infinite scroll effect by including some divs from another component. I have found an infinite scroll library online and I am using it here in the index.js file. I have created my divs in Home.js but I am getting some errors like:-
Error: Mandatory prop "refreshFunction" missing. and TypeError: this.props.next is not a function. Can someone help me out please. I think even though I am exporting Home.js and then importing it in index.js but it is still not able to invoke the methods. Can someone please let me know what I am doing wrong and how to correct it? I have added the code below.
index.js
import React from 'react';
import { render } from "react-dom";
import InfiniteScroll from 'react-infinite-scroll-component';
import { Home } from "./components/Home";
class App extends React.Component{
render() {
return (
<InfiniteScroll
pullDownToRefresh
pullDownToRefreshContent={
<h3 style={{ textAlign: 'center' }}>↓ Pull down to refresh</h3>
}
releaseToRefreshContent={
<h3 style={{ textAlign: 'center' }}>↑ Release to refresh</h3>
}
refreshFunction={this.refresh}
next={this.generateDivs}
hasMore={true}
loader={<h4>Loading...</h4>}
endMessage={
<p style={{ textAlign: 'center' }}>
<b>Yay! You have seen it all</b>
</p>
}>
<Home/>
</InfiniteScroll>
);
}
}
render(<App />, window.document.getElementById("app"));
Home.js
import React from "react";
const style = {
display: 'flex',
alignItems: 'center',
fontSize: 40,
border: '2px solid red',
margin: ' 0 auto 10px auto',
textAlign: 'center'
}
const divs = [
<div key={1} style={{ height: 200, ...style }}>Div no 1</div>,
<div key={2} style={{ height: 200, ...style }}>Div no 2</div>,
<div key={3} style={{ height: 200, ...style }}>Div no 3</div>,
<div key={4} style={{ height: 200, ...style }}>Div no 4</div>,
<div key={5} style={{ height: 200, ...style }}>Div no 5</div>,
<div key={6} style={{ height: 200, ...style }}>Div no 6</div>,
<div key={7} style={{ height: 200, ...style }}>Div no 7</div>,
<div key={8} style={{ height: 200, ...style }}>Div no 8</div>,
];
export class Home extends React.Component{
constructor() {
super();
this.state = {
divs: divs
};
this.generateDivs = this.generateDivs.bind(this);
this.refresh = this.refresh.bind(this);
}
generateDivs() {
let moreDivs = [];
let count = this.state.divs.length;
console.log(count);
for (let i = 0; i < 30; i++) {
moreDivs.push(
<div key={'div' + count++} style={{ height: 200, ...style }}>
Div no {count}
</div>
);
}
setTimeout(() => {
this.setState({ divs: this.state.divs.concat(moreDivs) });
}, 500);
}
refresh() {
this.setState({ divs: [] });
setTimeout(() => {
this.setState({ divs });
}, 3000);
}
render() {
return (
<div>
{this.state.divs}
</div>
);
}
}