FlatList in react native is rendering but not showing text - reactjs

I'm trying to render a flatlist by connecting to a internal JSON file. The flatlist seems to be rendering but not showing any text. The cardlist in the code is being rendered 9 times, there are 9 objects in the JSON file. But no text is showing.
// libraryList.js
import React, { Component } from 'react';
import { FlatList } from 'react-native';
import { connect } from 'react-redux';
import ListItem from './ListItem';
class LibraryList extends Component {
renderItem(library) {
return <ListItem library={library} />;
}
render() {
// console.log(this.props);
// return;
return (
<FlatList
data={this.props.libraries}
renderItem={this.renderItem}
keyExtractor={library => library.id}
/>
);
}
}
const mapStateToProps = state => {
return { libraries: state.libraries };
};
export default connect(mapStateToProps)(LibraryList);
// ListItem.js
import React, { Component } from 'react';
import { Text } from 'react-native';
import { CardSection } from './common';
class ListItem extends Component {
render() {
return (
<CardSection>
<Text>{this.props.library.title}</Text>
</CardSection>
);
}
}
export default ListItem;
import React, { Component } from 'react';
import { Text } from 'react-native';
import { CardSection } from './common';
class ListItem extends Component {
render() {
return (
<CardSection>
<Text>{this.props.library.title}</Text>
</CardSection>
);
}
}
export default ListItem;
Just want to list the title at this stage.

You need to modify renderItem because FlatList passes an object into the renderItem callback function.
Instead, use the below
renderItem = ({ item }) => <ListItem library={item} />

Thanks Dan you put me onto the right lines. I used
renderItem={({ item }) => this.renderItem(item)}

Related

React Native - render a component onClick on an Icon

A part of my app has an 'icon'. When I 'click' on the icon, the State of the parent component changes to 'true' and I want a new 'Component' to render saying 'I am a new Component'. I am trying like this below, there is no error showing at the debugger. The Icon is an image component that I am importing. Here is the Code.
This is the parent Component
import React, {Component} from 'react';
import {View} from 'react-native';
import {Icon} from '../ui/Icon';
type HomeSceneState = {
calendarState:boolean
}
class HomeScene extends Component<HomeSceneState> {
state = {
calendarState:false
}
openCalendar = () => {
console.log("open calendar")
this.setState({
calendarState : true
})
}
render() {
return (
<View style={{marginBottom: spacing.double,
backgroundColor:"black", flexDirection:"row"
}}>
<View>
<Icon onPress = {() => this.openCalendar()} />
{this.calendarState ? <Casie/> : null }
</View>
</View>
);
}
}
export default HomeScene;
The Children Component looks like below
class Casie extends Component<CalendarProps> {
render() {
return (
<View>
I am a new Component
</View>
);
}
}
export default Casie;
replace <Calendar/> by your child component name <Casie/> (in your case). It seems you are not rendering your child component when the state changes.
In your parent component you import the react native navigation.
after you can use useNavigation for navigate the page. Sorry my english.
import { useNavigation } from '#react-navigation/native'
class HomeScene extends Component<HomeSceneState> {
state = {
calendarState:false
}
openCalendar = () => {
console.log("open calendar")
navigation.navigate('Casie')
}
render() {
return (...)
}
}
export default HomeScene;

what is best way to get props in React / React Native

I'm really regarding props in React/React-Native. I have a parent view. In this view I'm getting the user data from a LocalStorage.['
import React, { Component } from 'react';
import { Container, Content, View } from 'native-base';
import NutrionalToolbar from '../../components/NutrionalToolbar';
import { AsyncStorage } from 'react-native';
export default class LogsScreen extends Component {
state = {
user: '',
}
componentWillMount() {
this._bootstrapAsync();
}
_bootstrapAsync = async () => {
const user = await AsyncStorage.getItem('user');
this.setState({ user: JSON.parse(user) })
};
render() {
return (
<Container>
<NutrionalToolbar user={this.state.user} />
</Container>
);
}
}
Now inside the NutrionalToolbar component I have this.
import React, { Component } from 'react';
import { View } from 'native-base';
class NutrionalToolbar extends Component {
constructor(props) {
super(props)
console.log(this.props) // This renders an empty user object
}
render() {
console.log(this.props) // This renders the user object with values
return (
<View>
</View>
);
}
}
export default NutrionalToolbar;
How can I get this.props values inside the constructor. I'm getting the values inside render method. Why isn't working inside the constructor?
I would recommend looking into the componentDidUpdate lifecycle hook because, even if you could access the initial user prop in the constructor, you wouldn't be able to access updates to that prop in the constructor.
import React, { Component } from 'react';
import { View } from 'native-base';
class NutrionalToolbar extends Component {
componentDidUpdate() {
console.log(this.props) // This will always log the current props
}
render() {
return (<View></View>);
}
}
export default NutrionalToolbar;

Why would a react-native view not update after render has successfully run?

I have a component DeckListView which I navigate to after updating state with redux. When I use the debugger in chrome I can see the this.props.Decks.map((deck) loop going through successfully with a list of data, but when I see the screen I don't see the additional Text. Any idea what may be happening?
I have what I believe to be the key code snippets below. The rest can be found at https://github.com/wcwcaseman/mobile-flashcards
Reducer
case ADD_DECK :
return {
...state,
[action.deck.title]:action.deck,
}
Navigation
homePage = () => {
this.props.navigation.navigate('DeckListView');
}
Actual page
import React, { Component } from 'react';
import { View, Text} from 'react-native';
import { connect } from 'react-redux'
class DeckListView extends Component {
render() {
return (
<View>
<Text>Toast the world</Text>
{this.props.Decks.map((deck) => {
<Text key={deck} >item</Text>
})}
</View>
);
}
}
function mapStateToProps ({ decks }) {
let Decks = [];
if(decks !== {} && decks !== null)
{
Decks = Object.keys(decks);
}
return {
Decks: Decks
}
}
export default connect(mapStateToProps)(DeckListView)
You need to return from the map function
{this.props.Decks.map((deck) => {
return <Text key={deck} >item</Text>
})}

React native infinite loop while pass the parameter into function

How to call function inside another function>
For example
I have 2 cascade dropdown which value from second dropdown depends on from first dropdown. inside the first dropdown there is a method like this
`onValueChange = {(value)=>{this.props.getSecondValue(value.id)}}.`
My question is why every time I change the value from first dropdown it causes infinite loop in this.props.getSecondValue() function?
How to solve that problem.
Many thanks !
update
here my snippet code
import React, {Component} from 'react';
import {StyleSheet, Text, View, TouchableOpacity, KeyboardAvoidingView, SafeAreaView, ScrollView} from 'react-native';
import {bindActionCreators} from "redux";
import {RootActions} from "../../shared/root-actions";
import connect from "react-redux/es/connect/connect";
import Config from 'react-native-config';
import _ from 'lodash';
import {Dropdown} from 'react-native-material-dropdown';
type Props = {};
const styles = StyleSheet.create({
container: {
flex: 1
}
});
class Index extends Component<Props> {
constructor(props) {
super(props);
this.state = {
point_type_id: '',
point_category_id: '',
}
this._getPointTypes = this._getPointTypes.bind(this);
this._getPointCategories = this._getPointCategories.bind(this);
}
componentDidMount() {
this._getPointTypes();
}
_getPointTypes(){
this.props.getPointTypes();
}
_getPointCategories(point_type_id){
this.props.getPointCategories(point_type_id);
}
render() {
let {
point_type_id,
point_category_id,
} = this.state;
let {
} = this.locationDetailStates;
return (
<View style={styles.container}>
....
<Dropdown
label='Point types'
data={point_types}
labelExtractor = {(value)=>{this._getPointCategories(value.id)}}
/>
<Dropdown
label='Point categories'
data={point_categories}
/>
</View>
);
}
}
function mapStateToProps(state) {
return {
outdoorStates: state.outdoorStates,
locationDetailStates:state.locationDetailStates
}
}
function mapDispatchToProp(dispatch) {
return bindActionCreators(RootActions, dispatch)
}
export default connect(mapStateToProps, mapDispatchToProp)(Index);
Try to refactor like this:
function mapDispatchToProps(dispatch) {
return {
getSecondValue: (id)=>{
//Your code here
}
}
}
class MyClass extends React.Component {
handleChange = (e)=>{
//assign your id from event values
this.props.getSecondValue(id);
}
...
render(){
return (
//inside a tag
onChange = {this.handleChange}
)
}
}

Warning: React.createElement: type should not be null

I'm creating my very first app with reactJS but I keep getting this error:
Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM element) or a ReactClass (for composite components). Check the render method of 'App'
I've already read a dozen of questions but none of them helped me get through this problem.
Here are my files:
my app.js file:
import React, { Component } from 'react';
import { View, Text } from 'react-native';
import { Header } from './components/common';
import { ChooseLevel } from './components/ChooseLevel';
class App extends Component {
renderContent() {
<ChooseLevel/>
}
render() {
return (
<View>
<Header headerText="my app" />
{this.renderContent()}
</View>
);
}
}
export default App;
ChooseLevel.js:
import React, { Component } from 'react';
import { Text, View } from 'react-native';
class ChooseLevel extends Component {
render() {
var rows = [];
for (var i=1; i <= 10; i++) {
rows.push(
<button style={styles.squareStyle}>
Level
</button>
);
}
return (
<div>
{rows}
</div>
);
}
}
const styles = {
squareStyle: {
flex:0.3,
height:60
}
};
export default ChooseLevel;
You are making a default export: export default ChooseLevel; but importing something else: import { ChooseLevel } from './components/ChooseLevel';
import { ChooseLevel } from './components/ChooseLevel'; roughly converts to const ChooseLevel = require('./components/ChooseLevel').ChooseLevel;
To import the default export value you'll need to import ChooseLevel from './components/Chooselevel';
Take a look at http://es6-features.org/#Constants it explains every ES6 feature in detail.

Resources