簡體   English   中英

上下文API:使用處於上下文狀態的函數來更改上下文變量。 功能未執行

[英]Context API: using a function in the context's state to change a context variable. Function isn't executing

-反應16.5-雖然我認為這個問題是一個反應性問題,而不是一個反應性本機問題,但這是我的反應性本機版本。 我的package.json沒有本機版本,但具有:“ react-native”:“ https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz

目標:從另一個文件更改上下文(Context API)中變量的值

方法:我在上下文中有一個函數來切換值和另一個文件中調用它的按鈕。

問題:當我按下按鈕時,該功能無法執行。 盡管我的上下文變量(稱為“ ctx”)在被調用的范圍內可用,但仍會發生這種情況。

這是它自己的文件中的上下文(我們將其稱為“上下文文件”):

class ConfigProvider extends Component {
  state = {
    dataSource: ds.cloneWithRows(game_array),

    toggleQuestion: () => {
      debugger;
      new_game_array = game_array[0].questions.completed = true;
      this.setState({ game_array: new_game_array });
    }
  };

這是函數的調用位置(我們將其稱為“按鈕文件”):

if (hold_type === "true_false"){
        debugger;
        return(
            <View style={{ width: dims.width, height: HEIGHT_OF_true_false_SECTION, flexDirection: "row", justifyContent: "space-around", alignItems: "center"}}>
                    <Button title='True!' color='red' onPress={()=>{ctx.toggleQuestion)}} />
                    <Button title='False' color='green' onPress={()=>{}} />
            </View>
        )
    }

我的經驗

當Button File進入debug語句時,我可以檢查變量“ ctx”,它完全可用(包括ctx.toggleQuestion)。 但是,該函數不會觸發,因為未捕獲上下文文件中的debug語句。

您如何為我提供幫助:

  • 解釋為什么它沒有被抓住
  • 告訴我我的方法是行得通還是行不通
  • 如果您知道更好的方法,請這樣說。

確定,如果您可以在檢查器中看到ctx.toggleQuestion,則錯誤是(除非您有錯字...)

onPress={()=>{ctx.toggleQuestion)}} (this line doesnt actually call the function. In fact it doesn't do anything)

應該

onPress={()=>{ctx.toggleQuestion()}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM