React-Native. js Переменная не найдена - PullRequest
2 голосов
/ 14 апреля 2020

Не могу понять, почему я продолжаю получать ReferenceError, не могу найти переменную MarkAsRead для мобильного приложения, которое я создаю в реакции. Если я не пропустил что-то, переменная была назначена ниже, это код для вашей ссылки. Надеюсь, кто-нибудь может помочь мне решить эту проблему своевременно, спасибо заранее!

   import React from 'react';
import { View, 
  Text, 
  StyleSheet, 
  SafeAreaView, 
  TouchableOpacity,
  TextInput,
  FlatList

} from 'react-native';

import BookCount from './components/BookCount';

import {Ionicons} from '@expo/vector-icons';

class App extends React.Component {

    constructor() {
      super()
      this.state = {
        totalCount: 0,
        readingCount: 0,
        readCount: 0,
        isAddNewBookVisible:false,
        textInputData: '',
        books: [],
        bookData: {
          author: '',
          publisher: ''
        }
      };
    }


      showAddNewBook = () => {
        this.setState({isAddNewBookVisible:true});


    };

    hideAddNewBook = () => {
      this.setState({isAddNewBookVisible:false})
    };

    addBook = book => {
      this.setState(
      (state, props) => ({
          books: [...state.books, book],
          totalCount: state.totalCount + 1,
          readingCount:state.readingCount + 1,
          isAddNewBookVisible: false

      }), 
      () => {
        console.log(this.state);
      }
      );

    };
  markAsRead = (selectedBook, index) => {
    let newList = this.state.books.filter(book => book !==
      selectedBook);

      this.setState(prevState => ({
        books: newList,
        readingCount: prevState.readingCount - 1,
        readCount: prevState.readCount + 1

      }));
    };

renderItem = (item, index) => (
  <View style={{ height:50, flexDirection: 'row'}}>
  <View style={{ flex:1, justifyContent: 'center', paddingLeft: 5
  }}>
    <Text>{item}</Text>
    </View>
    <TouchableOpacity onPress={() => markAsRead(item,index)} >
        <View 
        style={{
          width: 100,
          height: 50,
          alignItems: 'center',
          justifyContent: 'center', 
          backgroundColor: '#160b1a'
          }}

          >
        <Text style={{ fontWeight: 'bold', color: 'white'}}>Mark as Read</Text>

        </View>
        </TouchableOpacity>
  </View>

);
  render() {
    return ( 
    <View style={{flex: 1}}>
      <SafeAreaView/>
      <View style={{ 
        height: 70, 
        borderBottomWidth: 0.5,
        borderBottomColor: '#5e3c7d',
        alignItems: 'center',
        justifyContent: 'center'
      }} 
      >
      <Text style={{fontSize: 24}}>VekTorfy AI</Text>
      </View>
      <View style={{ flex: 1}}>
      {this.state.isAddNewBookVisible &&
      <View style={{height:50, flexDirection: 'row'}}>
        <TextInput
          onChangeText={(text)=>this.setState({textInputData:text})} 
          style={{ flex:1, backgroundColor: '#c6c0cb', 
          paddingLeft: 5}}
          placeholder='Enter book name.'
          placeholderTextColor='black'
        />

        <TouchableOpacity 
        onPress={() => this.addBook(this.state.textInputData)} >
        <View style={{
          width: 50,
          height: 50,
          alignItems: 'center',
          justifyContent: 'center', 
          backgroundColor: '#160b1a'}}>
        <Ionicons name ='ios-checkmark' color='white' size={40}/>

        </View>
        </TouchableOpacity>

        <TouchableOpacity onPress={this.hideAddNewBook}>
        <View style={{
          width: 50,
          height: 50,
          alignItems: 'center',
          justifyContent: 'center', 
          backgroundColor: '#160b1a'}}>
        <Ionicons name ='ios-close' color='red' size={40}/>

        </View>
        </TouchableOpacity>
      </View>
      }
      <FlatList
        data={this.state.books}
        renderItem={({item}, index) => this.renderItem(item, index)}
        keyExtractor={(item, index)=> index.toString()}
        ListEmptyComponent={
        <View style={{marginTop: 50, alignItems: 'center'}}>
        <Text style={{fontWeight: 'bold'}}>Not Reading anything.</Text>
        </View>
        }
      />
      <TouchableOpacity
      onPress={this.showAddNewBook}
      style={{position: 'absolute', bottom: 20, right: 20}}>
      <View 
      style={{
      width:50,
      heght:50,
      alignItems: 'center',
      justifyContent: 'center',
      borderRadius:25, 
      backgroundColor: '#2d2337'}}>
      <Text style={{color: 'white', fontSize: 30}}>+</Text>
      </View></TouchableOpacity>
      </View>
      <View
      style={{ 
        height: 70,
        flexDirection: 'row',
        borderTopWidth: 0.5,
        borderTopColor: '#5e3c7d' }}>

        <BookCount title='Total' count={this.state.totalCount}/>
        <BookCount title='Reading' count={this.state.readingCount}/>
        <BookCount title='Read' count={this.state.readCount}/>


        </View>
        <SafeAreaView/>
        </View>

    ); 
  }
}
export default App;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center'
  }

});

Ответы [ 2 ]

1 голос
/ 14 апреля 2020

Похоже, вы объявили markAsRead в качестве метода для вашего App класса, поэтому правильный способ ссылки на него - this.markAsRead()

<TouchableOpacity onPress={() => this.markAsRead(item, index)}>
1 голос
/ 14 апреля 2020

Вы забыли добавить ключевое слово this в вызов функции.

<TouchableOpacity onPress={() => this.markAsRead(item,index)}>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...