【问题标题】:How do I get this to work in React Native ( Logic )我如何让它在 React Native ( Logic ) 中工作
【发布时间】:2021-01-31 02:57:09
【问题描述】:

抱歉含糊不清,我是 react-native 的初学者,不到一周前就开始学习它。我没有收到错误,但我基本上希望当我按下开始时它会完成所有锻炼并告诉你有多少次代表,并让你每次按下按钮时删除 1 组。但问题是由于某种原因,锻炼和虚拟锻炼之间的关系很奇怪。什么都有帮助!例如当

dummyWorkouts is [
{Object},
{Object}
]
workouts is [
{Object},
{Object},
{Object},
{Object}
]

但是对于默认设置,我认为将 dummyWorkouts 设置为锻炼时会出现问题。如果有更好的方法来做到这一点,那将是非常有帮助的另一个大问题是,当它逐个浏览锻炼列表时。将其限制为 2。

具体如下

setCurrentSets(currentSets-1)
if(currentSets == 1){
   if(dummyWorkouts.length > 1){
        setCurrentWorkout(dummyWorkouts[1])
        setCurrentSets(dummyWorkouts[1].sets)
        var index = dummyWorkouts.indexOf(dummyWorkouts[0])
        setDummyWorkouts(dummyWorkouts.splice(index, 1))
     }else{
        setDummyWorkouts(workouts)
        setCurrentWorkout(workouts[0])
        setCurrentSets(workouts[0].sets)
        setWorkout(false)
   }
}
import React, { useState } from 'react';
import { StyleSheet, Text, View, FlatList, Alert, Button } from 'react-native';
import Header from './components/header';
import Workouts from './components/workouts';
import AddWorkout from './components/addWorkout';

export default function App() {
  const [workout, setWorkout] = useState(false);

  const [workouts, setWorkouts] = useState([
    { name: 'Pushups', key: '1', sets: '3', reps: '20' },
    { name: 'Test', key: '2', sets: '3', reps: '20' },
  ]);

  const [currentWorkout, setCurrentWorkout] = useState(workouts[0]);
  const [currentSets, setCurrentSets] = useState(3)
  const [dummyWorkouts, setDummyWorkouts] = useState();

  const pressHandler = (key) => {
    setWorkouts((prevWorkouts) => {
      return prevWorkouts.filter(workout => workout.key != key);
    })
  }

  const submitHandler = (text) => {
    if(text.length > 0){
      if(text.length <= 40){
        setWorkouts((workouts) => {
        return [
          { name: text, key: Math.random().toString(), reps: '10', sets: '3' },
          ...workouts
        ]
    })
      }else {Alert.alert('Whoops!', "You entered over 40 characters! Shorten it up a bit!", [{text: 'Close '}])}
    }else {Alert.alert('Whoops!', "You haven't entered anything!", [{text: 'Close '}])}
  }

  if(workout == false){
  return (
    <View style={styles.container}>
      <Header />
      <View style={styles.WorkoutButton}>
        <Button
          title='START'
          color='white'
          onPress={()=>{
            if(workouts.includes(workouts[0])){
              setDummyWorkouts([...workouts])
              setWorkout(true)
            }else{Alert.alert('Whoops!', "No workouts detected!", [{text: 'Close '}])}
          }}
        />
      </View>
      <View style={styles.content}>
        <AddWorkout submitHandler={(val) => {submitHandler}}/>
        <View style={styles.list}> 
          <FlatList 
            data={workouts}
            renderItem={({ item }) => (
              <Workouts item={item} pressHandler={pressHandler} />
            )}
          />
        </View>
      </View>
    </View>
  );}else if (workout == true  && workouts.includes(workouts[0])){
    return(
    <View style={styles.container}>
      <Header />
      <View style={styles.WorkoutButton}>
        <Button
          title='-'
          color='white'
          onPress={() => {
            setCurrentSets(currentSets-1)
            if(currentSets == 1){
              if(dummyWorkouts.length > 1){

                setCurrentWorkout(dummyWorkouts[1])
                setCurrentSets(dummyWorkouts[1].sets)
                var index = dummyWorkouts.indexOf(dummyWorkouts[0])
                setDummyWorkouts(dummyWorkouts.splice(index, 1)) //delete workout
              }else{
                setDummyWorkouts(workouts)
                setCurrentWorkout(workouts[0])
                setCurrentSets(workouts[0].sets)
                setWorkout(false)
              }
            }
          }}
        />
      </View>
      <View style={styles.WorkoutCard}>
        <Text style={styles.Workout}>
          {currentWorkout.name}
        </Text>
        <Text style={styles.Sets}>
          Sets Left
          {'\n'}
          {currentSets}
        </Text>
        <Text style={styles.Reps}>
          Reps
          {'\n'}
          {currentWorkout.reps}
        </Text>
      </View>
    </View>
    );}else{
        setWorkout(false)
        Alert.alert('Whoops!', "No workouts detected!", [{text: 'Close '}])
      }
}

//Style Sheet

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#3B3B3B',
  },
  content: {
    padding: 60,
    flex: 1,
  },
  list: {
    marginTop: 20,
    flex:1,
  },
  text: {
    color: 'white',

  },
  WorkoutButton: {
    backgroundColor: 'black',
    left: 140,
    bottom: 52,
    marginHorizontal: 150,
    borderRadius: 15,
    fontWeight: 'bold',
  },
  WorkoutCard: {
    backgroundColor: 'crimson',
    height: 600,
    width: 300,
    marginTop: 50,
    alignSelf: 'center',
    borderRadius: 50,
  },
  Workout: {
    fontSize: 50,
    fontWeight: '600',
    color: 'white',
    textAlign: 'center',
  },
  Sets: {
    fontSize: 50,
    fontWeight: '400',
    color: 'white',
    textAlign: 'center',
    top: 30,
  },
  Reps: {
    fontSize: 40,
    fontWeight: '400',
    color: 'white',
    textAlign: 'center',
    top: 50,
  },
});```

【问题讨论】:

  • 你能具体说明dummyworkouts和锻炼之间的关系吗
  • DummyWorkouts 基本上是作为锻炼的副本,我可以对其进行编辑而不会弄乱实际锻炼
  • 你能说明什么问题吗,我没明白。
  • 我已经回答了问题中的问题。

标签: javascript node.js react-native


【解决方案1】:

这个项目后来被放弃了,手头的问题是复制 dummyWorkouts(一个数组)。它必须做一些我觉得比我的编程更低端的事情,而且这仍然超出了我的专长。

【讨论】:

  • 我认为将“项目被放弃”标记为答案是不合适的。 SO 的主要目标之一是帮助未来有类似问题的开发人员;通过接受这个作为答案,您明确地对那些开发人员说,最好的前进道路是放弃整个项目。投反对票。
  • @Tom 我明白这一点,但老实说,我不太确定该怎么做。 (几个月后我回来,发现自己被禁止在 stackoverflow 上提问,我正在尽我所能来修复我的声誉。)这些问题是拖累我声誉的一些事情,我需要解决这个问题。
  • 获得一些初始声誉的最佳方法可能是回答问题或编辑问题和答案。使用错误的网站将自己从声誉漏洞中挖掘出来最终会适得其反。见stackoverflow.com/help/whats-reputation
  • 我建议让这个问题对您产生正面影响的一种方法是发布一个实际的解决方案。这个答案表明您确实弄清楚了,但您没有详细说明,因此未来的开发人员将从中获得零价值。如果你对这个问题有足够的了解来写下这个答案,那就去做吧!你会得到我的支持,也许还有其他人的支持。
猜你喜欢
  • 2020-08-08
  • 2015-12-17
  • 1970-01-01
  • 2016-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-02
  • 1970-01-01
相关资源
最近更新 更多