import React from 'react' import { View, Text, ListView, RefreshControl, TouchableOpacity } from 'react-native' const ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => { return r1.name != r2.name } }) const defState = () => ({ show: false, list: [], id: '', result: null }) const shuffle = (array) => { var currentIndex = array.length, temporaryValue, randomIndex; // While there remain elements to shuffle... while (0 !== currentIndex) { // Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1; // And swap it with the current element. temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue; } return array; } export default class ItemsPage extends React.Component { constructor(props) { super(props) this.state = defState() this.refreshList = this.refreshList.bind(this) this.randomItem = this.randomItem.bind(this) } refreshList() { let { getList } = this.props getList(this.state.id) } randomItem() { let ls = shuffle([...this.state.list]) this.setState({ result: ls[0] || null }) } componentWillReceiveProps(nextProps) { if (this.state.show !== nextProps.show) { this.setState({ show: nextProps.show, result: null }) } if (this.state.id !== nextProps.sid) { this.setState({ id: nextProps.sid }, () => { this.refreshList() }) } this.setState({ list: nextProps.list }) } render() { let { dev, list, refresh, getList, backPage } = this.props if (!this.state.show) return null console.log('Items') return ( backPage()}> <<Back this.randomItem()}> Random!! {this.state.result != null ? 結果: {this.state.result.name} / {this.state.result.price} : null} } enableEmptySections={true} dataSource={ds.cloneWithRows(list)} renderRow={data => { return ( {data.name} / {data.price} ) }} /> ) } }