Соединение страницы json с гибкой строкой в ​​React-native - PullRequest
1 голос
/ 14 мая 2019

У меня проблема с строкой flex в реагировать на нативный, поэтому мне нужно использовать только один файл данных json для генерации списка.Тем не менее, я действительно не могу понять, как.

Я только что создал 2 отдельных JSON, но проблема в том, что они просто перечисляют с задержкой друг к другу.И мне нужен только один.

 export default class Detay extends React.Component {

        constructor(props) {
      super(props);
      this.state = {
        ApiTitle: [],
        ApiTitle2: []
            }
    }
    componentDidMount() {
      axios.get('http://oyleboyle.com/data.json')
      .then(response => {
        this.setState({ ApiTitle: response.data.aparatifler });
      })
      .catch(error => {
        console.log(error);
      });
      axios.get('http://oyleboyle.com/data2.json')
      .then(response => {
        this.setState({ ApiTitle2: response.data.aparatifler });
      })
      .catch(error => {
        console.log(error);
      });

    }


    renderItem(){

    }

    render() {
      return (
        <View style={{backgroundColor: "white"}}>
        <ScrollView>
        <View style={styles.flexview}>
        <View>{this.state.ApiTitle.map((id, i)=>
            <Urun title={id.title} image="https://nelazimsa.carrefoursa.com/wp-content/uploads/2018/03/turk-kahvesi.jpg" fiyat="12"/>

           )}
          </View>

        <View>{this.state.ApiTitle2.map((id, i)=>

            <Urun title={id.title} image="https://nelazimsa.carrefoursa.com/wp-content/uploads/2018/03/turk-kahvesi.jpg" fiyat="12"/>

        )}
          </View>

          </View>

          </ScrollView>
          </View>
      );
    }
  }

 const styles = StyleSheet.create({
        flexview: {
        flex: 1, flexDirection: 'row' , 
        marginTop: 10 , 
        justifyContent:'space-around'
        },
        img: {
          width: 280, 
          height: 280, 
          alignItems: 'center', 
          overflow: 'hidden'
        },
        titlee: {
          textAlign: 'center', 
          color: 'red', 
          fontSize: 18
        },
        fiyatt: {
          textAlign: 'center', 
          marginTop: 5
        },
        sepett: {
          color: 'white' ,
          textAlign: 'center', 
          marginTop: 5, 
          fontSize: 22 , 
          backgroundColor: 'red', 
          borderRadius: 7
        },
        kart: {
          borderRadius: 8, 
          padding: 5
        }
      });

Я использую строку, и мне нужно одновременно перечислить два столбца только с одним файлом json

1 Ответ

1 голос
/ 14 мая 2019

Вы можете объединить ваши два массива в один, выполнив следующие действия:

 //copy the old state of ApiTitle and add new items from response.data
 this.setState({ ApiTitle: [...this.state.ApiTitle, ...response.data.aparatifler]});

Полный код:

componentDidMount(){
      axios.get('http://oyleboyle.com/data.json')
      .then(response => {
        // here we are copying the content of ApiTitle and the content of repsonse.data together 
        this.setState({ ApiTitle: [...this.state.ApiTitle, ...response.data.aparatifler]});
      })
      .catch(error => {
        console.log(error);
      });
      axios.get('http://oyleboyle.com/data2.json')
      .then(response => {
        // here we are copying the content of ApiTitle and the content of repsonse.data together again 
        this.setState({ ApiTitle: [...this.state.ApiTitle, ...response.data.aparatifler]});
      })
      .catch(error => {
        console.log(error);
      });
    }

Затем вы сможете визуализировать ваш объединенный массивс плоским списком.

renderItem(item, index) {
  // render your items in return 
  return (
    <View key={index} style={{flex: 1}}>
        <Text> id: {item.id} </Text>
        <Text> titel: {item.title} </Text>
        <Text> fiyat: {item.fiyat} </Text>
    </View>
  )
}

render() {
    return (
      <View style={styles.container}>
        <FlatList
        data={this.state.ApiTitle}
        numColumns={2}
        renderItem={({ item, index }) => this.renderItem(item, index)}
        />
      </View>
    );
  }

Демонстрационный вывод:

demo

Пример работы:

https://snack.expo.io/Bk95rc_nE

...