0

問題 私はReact NativeとFirebaseを使用して簡単なアプリを作成しており、ユーザーはFirebaseに投稿できるようになり、アプリはすべての投稿を表示します。私は、アプリケーションの最初のアルファ版を作っている、私は手動でテキストの入力ボックスを介してそこに画面に合うように投稿サイズを調整している。私はテストのために電話機に保存されているプロトタイプポストを1つ持っています。テキスト入力の変数を変更するとプロトタイプポストは変更されますが、実際のポストは変更されません。私は、あなたがpostWidth変数を編集するときにFlatListの投稿がサイズを変更するのを助けるのが好きです。ありがとう!あなたは...このようなあなたの初期状態の宣言に投稿はアプリケーション内からサイズが変更されません

state = { 
    fontLoaded: false, 
    postWidth = 350, 
} 

をごを移動する必要がありますように

コード

import React, { Component } from 'react'; 
import { StyleSheet, Text, View, Image, TextInput, ScrollView, TouchableHighlight, Button, FlatList } from 'react-native'; 
import { Font } from 'expo'; 
import * as firebase from 'firebase'; 


const firebaseConfig = { 
    apiKey: "API-key", 
    authDomain: "candidtwo.firebaseapp.com", 
    databaseURL: "https://candidtwo.firebaseio.com", 
    storageBucket: "candidtwo.appspot.com", 
}; 

const firebaseApp = firebase.initializeApp(firebaseConfig); 

var fontLoaded = false; 

var ref = firebase.database().ref('posts'); 



var newPostRef = ref.push(); 

var postWidth = 350; 

export default class App extends React.Component { 

    state = { 
    fontLoaded: false, 
    }; 



    componentDidMount() { 
     Expo.Font.loadAsync({ 
     'JosefinSans-Regular.ttf': require('./JosefinSans-Regular.ttf'), 
     }); 
} 
    constructor(props) { 
    super(props); 
    this.state = { postInput: ""} 
} 

    componentWillMount(){ 
    this.getItems(); 
} 

    getItems(){ 
    var items = []; 
    var query = ref.orderByKey(); 
    query.once ('value', (snap) => { 
     snap.forEach ((child) => {  
     items.push(
     child.val().content 
     );  
     }); 
    items.reverse(); 
    }).then(() => { 
     this.setState({firebaseItems: items}); 
    }); 
} 


render() { 
    return (
     <View style={styles.container}> 
     <View style={styles.button}> 
      <View style={{width: 1, height: 30, backgroundColor: '#e8e8e8'}} /> 
      <Button 
      onPress={() => this.setState({ fontLoaded: true })} 
      title="Get started anonymously!" 
      color="#fe8200" 
      accessibilityLabel="Run the app" 
      /> 
     </View> 

     {this.state.fontLoaded ? (
      <View style={styles.container}> 
      <Text style={{ fontFamily: 'JosefinSans-Regular.ttf', fontSize: 16 }}> 
       Whats on your mind? Create a post! 
      </Text> 

      <TextInput 
       style={{height:40, width: 320, borderColor: '#303030', borderWidth: 1}} 
       onChangeText={(postInput)=>this.setState({postInput})} 
       value={this.state.postInput}  
      /> 


    <Button 
     style={{justifyContent: 'center'}} 
     onPress={() => { 
     newPostRef.set({ content:this.state.postInput }); 
     this.setState({ postInput: "Your post was succsesfully uploaded! :)" })  
     }}    
     title=" + " 
     color="#fe8200" 
    /> 

<View style={{width: 2500, height: 4, backgroundColor: '#a9a9a9'}}></View> 


      <ScrollView> 

<Text style={{ fontFamily: 'JosefinSans-Regular.ttf', fontSize: 16 }}> 
       Adjust the size of posts: 
      </Text> 

      <TextInput 
       style={{height:40, width: 100, borderColor: '#303030', borderWidth: 1}} 
       onChangeText={(postWidth)=>this.setState({postWidth})} 
       value={this.state.postWidth}  
      /> 



       <View style={{width: 1, height: 6, backgroundColor: '#e8e8e8'}} /> 
       <View style={{width: parseInt(this.state.postWidth), height: 250, backgroundColor: '#1daff1', alignItems: 'center', justifyContent: 'center', borderRadius: 10, paddingLeft: 10, paddingRight:10}} > 
     <Text style={{ fontFamily: 'JosefinSans-Regular.ttf', fontSize: 22, color: '#ffffff', textAlign: 'center'}}> 
        Prototype Post: Why do android phones have higher inital quality than apple phones, but apple phones have a more consistent amount of quality throughout their years? 
       </Text> 
      </View> 
       <View style={{width: parseInt(this.state.postWidth), height: 40, borderRadius: 10, backgroundColor: '#147aa8', flexDirection: 'row',paddingLeft:5}} > 
      <Image source={require('./CandidtwoImages/unlove.png')} /> 
      <Text style={{ fontFamily: 'JosefinSans-Regular.ttf', fontSize: 22, color: '#ffffff', paddingRight: 5, paddingTop:5}}> 
        3 
        </Text> 
      <Image source={require('./CandidtwoImages/undislike.png')} /> 
      <Text style={{ fontFamily: 'JosefinSans-Regular.ttf', fontSize: 22, color: '#ffffff', paddingRight: 5, paddingTop:5}}> 
        1 
        </Text> 
      <Image source={require('./CandidtwoImages/comments.png')} /> 
      <Text style={{ fontFamily: 'JosefinSans-Regular.ttf', fontSize: 22, color: '#ffffff', paddingRight: 5, paddingTop:5}}> 
        8 
        </Text> 
     </View> 
    <View style={{width: 1, height: 6, backgroundColor: '#e8e8e8'}} /> 


    <FlatList 
     data = {this.state.firebaseItems} 
     renderItem={({ item, index }) => 
    <View> 
       <View style={{width: parseInt(this.state.postWidth), height: 250, backgroundColor: '#1daff1', alignItems: 'center', justifyContent: 'center', borderRadius: 10, paddingLeft: 10, paddingRight:10}}> 
        <Text style={{ fontFamily: 'JosefinSans-Regular.ttf', fontSize: 22, color: '#ffffff', textAlign: 'center'}}> 
         { item } 
        </Text> 
       </View> 

       <View style={{width: parseInt(this.state.postWidth), height: 40, borderRadius: 10, backgroundColor: '#147aa8', flexDirection: 'row',paddingLeft:5}} > 

       </View> 
       <View style={{width: 1, height: 6, backgroundColor: '#e8e8e8'}} /> 
      </View> 
    } 
    /> 
     </ScrollView> 
     </View>) : (null) } 
     </View> 
    ); 
    } 
} 


const styles = StyleSheet.create({ 
    container: { 
    flex: 8, 
    backgroundColor: '#e8e8e8', 
    alignItems: 'center' 
    }, 
    button: { 
    flex: 1, 
    backgroundColor: '#e8e8e8', 
    alignItems: 'center' 
    }, 
}); 

答えて

0

は見えますが、それはおそらくあなたの問題ではありません。私がやるかもしれない次の事はあなたがちょうどそうのような単一の変数を使用できるようにreturn文の上のすべてのごparseInt(this.state.postWidth)を移動するためのリファクタリングです...

render() { 
    let postWidth = parseInt(this.state.postWidth); 
    return(
     <View style={{width: postWidth}} /> 
); 
} 

は、私はどちらかあなたの問題を解決するために起こっているとは思いませんしかし、それらは私がコメントするために見つけることができた唯一のものでした。あなたのレンダリングメソッドには(あまりにも多くの)実際に行われていることがたくさんあります。リファクタリングを少し簡略化し、これらのヒントが役立たない場合は、おそらくリファクタリングによって若干の光を放つでしょう。

1つのシンプルなビューの幅を変更するなど、何かを超簡単に試してみることをおすすめします。その後、さらに追加を開始します。

幸運を祈る!

関連する問題