React Context is not updating Data.

huangapple go评论99阅读模式
英文:

React Context is not updating Data

问题

以下是您要翻译的内容:

  1. I have created a React Context API, I have stored information such as first_name, hobbies, DOB etc in it. I have created a state in the context file and a function that changes the state when called. I am using the context in SignUpForm1.js and SignUpForm2.js, I am trying to update the state of context variables whenever there is a change in the text field, but the data in the context is not updating.
  2. UserData.js (Context)
  3. ```javascript
  4. import React from "react";
  5. import { useState } from "react";
  6. import {Text} from 'react-native';
  7. import { createContext } from "react";
  8. const SignUpContext = React.createContext({});
  9. const UserData = (props)=>{
  10. var state = {
  11. pref_pronoun: "",
  12. DOB: "",
  13. course: "",
  14. first_name: "",
  15. hobby_1: "",
  16. hobby_2: "",
  17. hobby_3: "",
  18. hobby_4: "",
  19. hobby_5: "",
  20. home_city: "",
  21. last_name: "",
  22. nationality: "",
  23. student_email: "",
  24. university: "",
  25. }
  26. const [userDetails , setDetails] = useState(state);
  27. const updateFormData = (field, value) => {
  28. setDetails({ [field]: value });
  29. console.log(state);
  30. };
  31. return (
  32. <SignUpContext.Provider value = {{state, updateFormData}}>
  33. {props.children}
  34. </SignUpContext.Provider>
  35. )
  36. }
  37. export {SignUpContext, UserData} ;

SignUpForm1.js

  1. import {
  2. Image,
  3. Text,
  4. StyleSheet,
  5. View,
  6. StatusBar,
  7. ScrollView,
  8. RefreshControl,
  9. } from "react-native";
  10. import DropDown from "./DropDown";
  11. import Input from "./Input";
  12. import {
  13. KeyboardAvoidingView,
  14. TouchableWithoutFeedback,
  15. Keyboard,
  16. } from "react-native";
  17. import { useCallback, useContext, useState } from "react";
  18. import CustomButton from "./CustomButton";
  19. import { useNavigation } from "@react-navigation/native";
  20. import DateTimePickerModal from "react-native-modal-datetime-picker";
  21. import { Button } from "react-native";
  22. import { SignUpContext, UserData } from "./GlobalUtil/UserData";
  23. const HideKeyboard = ({ children }) => (
  24. <TouchableWithoutFeedback onPress={() => Keyboard.dismiss()}>
  25. {children}
  26. </TouchableWithoutFeedback>
  27. );
  28. function SignUpForm1() {
  29. const [isDatePickerVisible, setDatePickerVisibility] = useState(false);
  30. const s_context = useContext(SignUpContext);
  31. const showDatePicker = () => {
  32. setDatePickerVisibility(true);
  33. };
  34. const hideDatePicker = () => {
  35. setDatePickerVisibility(false);
  36. };
  37. const navigation = useNavigation();
  38. const NationalityData = ["Football", "Cricket", "Programming", "Coding"];
  39. const [refreshing, setRefreshing] = useState(false);
  40. const onRefresh = useCallback(() => {
  41. setRefreshing(true);
  42. setTimeout(() => {
  43. setRefreshing(false);
  44. }, 2000);
  45. }, []);
  46. const statusBarHeight = Platform.OS === "ios" ? 50 : StatusBar.currentHeight;
  47. return (
  48. <KeyboardAvoidingView behavior="padding">
  49. <HideKeyboard>
  50. <View
  51. style={{
  52. height: "100%",
  53. width: "100%",
  54. backgroundColor: "#f1be63",
  55. }}
  56. >
  57. <View
  58. style={{ backgroundColor: "#f1be63", height: statusBarHeight }}
  59. >
  60. <StatusBar barStyle="dark-content" />
  61. </View>
  62. <ScrollView
  63. contentContainerStyle={styles.rootContainer}
  64. refreshControl={
  65. <RefreshControl
  66. refreshing={refreshing}
  67. onRefresh={onRefresh}
  68. />
  69. }
  70. >
  71. <Image
  72. source={require("../assets/aeroplane.png")}
  73. style={styles.image}
  74. resizeMode="contain"
  75. />
  76. <Text style={styles.header}>Let's get you set up</Text>
  77. <Text style={styles.lowerHeader}>
  78. (we promise that it won't take long)
  79. </Text>
  80. <View style={[styles.textFieldsContainer]}>
  81. <View style={{ alignItems: "center" }}>
  82. <Input
  83. isLabel={true}
  84. label="Preferred Pronoun"
  85. placeholder="He/Him"
  86. onChangeText={(text) => {
  87. s_context.updateFormData("pref_pronoun", text);
  88. }}
  89. />
  90. <Input
  91. isLabel={true}
  92. label="First Name"
  93. placeholder="Cameron"
  94. onChangeText={(text) => {
  95. s_context.updateFormData("first_name", text);
  96. }}
  97. />
  98. <Input
  99. isLabel={true}
  100. label="Last Name"
  101. placeholder="Cox"
  102. onChangeText={(text) => {
  103. s_context.updateFormData("last_name", text);
  104. }}
  105. />
  106. <View
  107. style={{
  108. backgroundColor: "white",
  109. width: "80%",
  110. borderRadius: 5,
  111. marginTop: 10,
  112. }}
  113. >
  114. <Button
  115. title="Date of Birth"
  116. onPress={showDatePicker}
  117. color="gray"
  118. />
  119. </View>
  120. <DateTimePickerModal
  121. isVisible={isDatePickerVisible}
  122. mode="date"
  123. onConfirm={(date) => {
  124. s_context.updateFormData('dob', date);
  125. hideDatePicker();
  126. }}
  127. onCancel={hideDatePicker}
  128. buttonTextColorIOS="white"
  129. pickerContainerStyleIOS={{ backgroundColor: "#D89D35" }}
  130. isDarkModeEnabled
  131. />
  132. </View>
  133. <View style={{ alignItems: "center" }}>
  134. <DropDown
  135. data={NationalityData}
  136. placeholder="Nationality"
  137. onSelect={(selectedItem, index) => {
  138. s_context.updateFormData("nationality", selectedItem);
  139. }}
  140. />
  141. <DropDown
  142. data={NationalityData}
  143. placeholder="University"
  144. onSelect={(selectedItem, index) => {
  145. s_context.updateFormData("university", selectedItem);
  146. }}
  147. />
  148. <DropDown
  149. data={NationalityData}
  150. placeholder="Course"
  151. onSelect={(selectedItem, index) => {
  152. s_context.updateFormData("course", selectedItem);
  153. }}
  154. />
  155. <DropDown
  156. data={NationalityData}
  157. placeholder="HomeTown City"
  158. onSelect={(selectedItem, index) => {
  159. s_context.updateFormData("homeCity", selectedItem);
  160. }}
  161. />
  162. <CustomButton
  163. isBorder={true}
  164. title="Next"
  165. onPress={() => {
  166. navigation.navigate("SignUp2");
  167. }}
  168. />
  169. </View>
  170. </View>
  171. </ScrollView>
  172. </View>
  173. </HideKeyboard>
  174. </KeyboardAvoidingView>
  175. );
  176. }
  177. const styles = StyleSheet.create({
  178. rootContainer: {
  179. height: "125%",
  180. justifyContent: "flex-start",
  181. alignItems: "center",
  182. marginTop: 24,
  183. },
  184. textFieldsContainer: {
  185. width: "100%",
  186. flex: 1,
  187. },
  188. image: {
  189. width: "25%",
  190. height: "10%",
  191. marginTop: 24,
  192. },
  193. header: {
  194. color: "white",
  195. fontSize: 26,
  196. fontWeight: "bold",
  197. marginVertical: 6,
  198. },
  199. lowerHeader: {
  200. color: "white",
  201. fontSize: 12,
  202. marginBottom: 24,
  203. },
  204. });
  205. export default SignUpForm1;
英文:

I have created a React Context API, I have stored information such as first_name, hobbies, DOB etc in it. I have crated a state in the context file and a function that changes the state when called. I am using the context in SignUpForm1.js and SignUpForm2.js, I am trying to update the state of context variables when ever there is a change in the text field, but the data in the context is not updating.

UserData.js (Context)

  1. import React from &quot;react&quot;;
  2. import { useState } from &quot;react&quot;;
  3. import {Text} from &#39;react-native&#39;;
  4. import { createContext } from &quot;react&quot;;
  5. const SignUpContext = React.createContext({});
  6. const UserData = (props)=&gt;{
  7. var state = {
  8. pref_pronoun: &quot;&quot;,
  9. DOB: &quot;&quot;,
  10. course: &quot;&quot;,
  11. first_name: &quot;&quot;,
  12. hobby_1: &quot;&quot;,
  13. hobby_2: &quot;&quot;,
  14. hobby_3: &quot;&quot;,
  15. hobby_4: &quot;&quot;,
  16. hobby_5: &quot;&quot;,
  17. home_city: &quot;&quot;,
  18. last_name: &quot;&quot;,
  19. nationality: &quot;&quot;,
  20. student_email: &quot;&quot;,
  21. university: &quot;&quot;,
  22. }
  23. const [userDetails , setDetails] = useState(state);
  24. const updateFormData = (field, value) =&gt; {
  25. setDetails({ [field]: value });
  26. console.log(state);
  27. };
  28. return (
  29. &lt;SignUpContext.Provider value = {{state, updateFormData}}&gt;
  30. {props.children}
  31. &lt;/SignUpContext.Provider&gt;
  32. )
  33. }
  34. export {SignUpContext, UserData} ;

SignUpForm1.js

  1. import {
  2. Image,
  3. Text,
  4. StyleSheet,
  5. View,
  6. StatusBar,
  7. ScrollView,
  8. RefreshControl,
  9. } from &quot;react-native&quot;;
  10. import DropDown from &quot;./DropDown&quot;;
  11. import Input from &quot;./Input&quot;;
  12. import {
  13. KeyboardAvoidingView,
  14. TouchableWithoutFeedback,
  15. Keyboard,
  16. } from &quot;react-native&quot;;
  17. import { useCallback, useContext, useState } from &quot;react&quot;;
  18. import CustomButton from &quot;./CustomButton&quot;;
  19. import { useNavigation } from &quot;@react-navigation/native&quot;;
  20. import DateTimePickerModal from &quot;react-native-modal-datetime-picker&quot;;
  21. import { Button } from &quot;react-native&quot;;
  22. import { SignUpContext, UserData } from &quot;./GlobalUtil/UserData&quot;;
  23. const HideKeyboard = ({ children }) =&gt; (
  24. &lt;TouchableWithoutFeedback onPress={() =&gt; Keyboard.dismiss()}&gt;
  25. {children}
  26. &lt;/TouchableWithoutFeedback&gt;
  27. );
  28. function SignUpForm1() {
  29. const [isDatePickerVisible, setDatePickerVisibility] = useState(false);
  30. const s_context = useContext(SignUpContext);
  31. const showDatePicker = () =&gt; {
  32. setDatePickerVisibility(true);
  33. };
  34. const hideDatePicker = () =&gt; {
  35. setDatePickerVisibility(false);
  36. };
  37. const navigation = useNavigation();
  38. const NationalityData = [&quot;Football&quot;, &quot;Cricket&quot;, &quot;Programmming&quot;, &quot;Coding&quot;];
  39. const [refreshing, setRefreshing] = useState(false);
  40. const onRefresh = useCallback(() =&gt; {
  41. setRefreshing(true);
  42. setTimeout(() =&gt; {
  43. setRefreshing(false);
  44. }, 2000);
  45. }, []);
  46. const statusBarHeight = Platform.OS === &quot;ios&quot; ? 50 : StatusBar.currentHeight;
  47. return (
  48. &lt;KeyboardAvoidingView behavior=&quot;padding&quot;&gt;
  49. &lt;HideKeyboard&gt;
  50. &lt;View
  51. style={{
  52. height: &quot;100%&quot;,
  53. width: &quot;100%&quot;,
  54. backgroundColor: &quot;#f1be63&quot;,
  55. }}
  56. &gt;
  57. &lt;View
  58. style={{ backgroundColor: &quot;#f1be63&quot;, height: statusBarHeight }}
  59. &gt;
  60. &lt;StatusBar barStyle=&quot;dark-content&quot; /&gt;
  61. &lt;/View&gt;
  62. &lt;ScrollView
  63. contentContainerStyle={styles.rootContainer}
  64. refreshControl={
  65. &lt;RefreshControl
  66. refreshing={refreshing}
  67. onRefresh={onRefresh}
  68. /&gt;
  69. }
  70. &gt;
  71. &lt;Image
  72. source={require(&quot;../assets/aeroplane.png&quot;)}
  73. style={styles.image}
  74. resizeMode=&quot;contain&quot;
  75. /&gt;
  76. &lt;Text style={styles.header}&gt;Let&#39;s get you set up&lt;/Text&gt;
  77. &lt;Text style={styles.lowerHeader}&gt;
  78. (we promise that it won&#39;t take long)
  79. &lt;/Text&gt;
  80. &lt;View style={[styles.textFieldsContainer]}&gt;
  81. &lt;View style={{ alignItems: &quot;center&quot; }}&gt;
  82. &lt;Input
  83. isLabel={true}
  84. label=&quot;Preferred Pronoun&quot;
  85. placeholder=&quot;He/Him&quot;
  86. onChangeText={(text) =&gt; {
  87. s_context.updateFormData(&quot;pref_pronoun&quot;, text);
  88. }}
  89. /&gt;
  90. &lt;Input
  91. isLabel={true}
  92. label=&quot;First Name&quot;
  93. placeholder=&quot;Cameron&quot;
  94. onChangeText={(text) =&gt; {
  95. s_context.updateFormData(&quot;first_name&quot;, text);
  96. }}
  97. /&gt;
  98. &lt;Input
  99. isLabel={true}
  100. label=&quot;Last Name&quot;
  101. placeholder=&quot;Cox&quot;
  102. onChangeText={(text) =&gt; {
  103. s_context.updateFormData(&quot;last_name&quot;, text);
  104. }}
  105. /&gt;
  106. &lt;View
  107. style={{
  108. backgroundColor: &quot;white&quot;,
  109. width: &quot;80%&quot;,
  110. borderRadius: 5,
  111. marginTop: 10,
  112. }}
  113. &gt;
  114. &lt;Button
  115. title=&quot;Date of Birth&quot;
  116. onPress={showDatePicker}
  117. color=&quot;gray&quot;
  118. /&gt;
  119. &lt;/View&gt;
  120. &lt;DateTimePickerModal
  121. isVisible={isDatePickerVisible}
  122. mode=&quot;date&quot;
  123. onConfirm={(date) =&gt; {
  124. s_context.updateFormData(&#39;dob&#39;, date);
  125. hideDatePicker();
  126. }}
  127. onCancel={hideDatePicker}
  128. buttonTextColorIOS=&quot;white&quot;
  129. pickerContainerStyleIOS={{ backgroundColor: &quot;#D89D35&quot; }}
  130. isDarkModeEnabled
  131. /&gt;
  132. &lt;/View&gt;
  133. &lt;View style={{ alignItems: &quot;center&quot; }}&gt;
  134. &lt;DropDown
  135. data={NationalityData}
  136. placeholder=&quot;Nationality&quot;
  137. onSelect={(selectedItem, index) =&gt; {
  138. s_context.updateFormData(&quot;nationality&quot;, selectedItem);
  139. }}
  140. /&gt;
  141. &lt;DropDown
  142. data={NationalityData}
  143. placeholder=&quot;University&quot;
  144. onSelect={(selectedItem, index) =&gt; {
  145. s_context.updateFormData(&quot;university&quot;, selectedItem);
  146. }}
  147. /&gt;
  148. &lt;DropDown
  149. data={NationalityData}
  150. placeholder=&quot;Course&quot;
  151. onSelect={(selectedItem, index) =&gt; {
  152. s_context.updateFormData(&quot;course&quot;, selectedItem);
  153. }}
  154. /&gt;
  155. &lt;DropDown
  156. data={NationalityData}
  157. placeholder=&quot;HomeTown City&quot;
  158. onSelect={(selectedItem, index) =&gt; {
  159. s_context.updateFormData(&quot;homeCity&quot;, selectedItem);
  160. }}
  161. /&gt;
  162. &lt;CustomButton
  163. isBorder={true}
  164. title=&quot;Next&quot;
  165. onPress={() =&gt; {
  166. navigation.navigate(&quot;SignUp2&quot;);
  167. }}
  168. /&gt;
  169. &lt;/View&gt;
  170. &lt;/View&gt;
  171. &lt;/ScrollView&gt;
  172. &lt;/View&gt;
  173. &lt;/HideKeyboard&gt;
  174. &lt;/KeyboardAvoidingView&gt;
  175. );
  176. }
  177. const styles = StyleSheet.create({
  178. rootContainer: {
  179. height: &quot;125%&quot;,
  180. justifyContent: &quot;flex-start&quot;,
  181. alignItems: &quot;center&quot;,
  182. marginTop: 24,
  183. },
  184. textFieldsContainer: {
  185. width: &quot;100%&quot;,
  186. flex: 1,
  187. },
  188. image: {
  189. width: &quot;25%&quot;,
  190. height: &quot;10%&quot;,
  191. marginTop: 24,
  192. },
  193. header: {
  194. color: &quot;white&quot;,
  195. fontSize: 26,
  196. fontWeight: &quot;bold&quot;,
  197. marginVertical: 6,
  198. },
  199. lowerHeader: {
  200. color: &quot;white&quot;,
  201. fontSize: 12,
  202. marginBottom: 24,
  203. },
  204. });
  205. export default SignUpForm1;

SignUpForm2.js

  1. import {
  2. View,
  3. Text,
  4. TouchableWithoutFeedback,
  5. StyleSheet,
  6. StatusBar,
  7. ScrollView,
  8. } from &quot;react-native&quot;;
  9. import { KeyboardAvoidingView } from &quot;react-native&quot;;
  10. import { Avatar } from &quot;react-native-elements&quot;;
  11. import Input from &quot;./Input&quot;;
  12. import DropDown from &quot;./DropDown&quot;;
  13. import { Keyboard } from &quot;react-native&quot;;
  14. import { Platform } from &quot;react-native&quot;;
  15. import { useNavigation } from &quot;@react-navigation/native&quot;;
  16. import CustomButton from &quot;./CustomButton&quot;;
  17. import { useState, useContext } from &quot;react&quot;;
  18. import { createNewUser } from &quot;./util/auth&quot;;
  19. import { SignUpContext, UserData } from &quot;./GlobalUtil/UserData&quot;;
  20. const HideKeyboard = ({ children }) =&gt; (
  21. &lt;TouchableWithoutFeedback onPress={() =&gt; Keyboard.dismiss()}&gt;
  22. {children}
  23. &lt;/TouchableWithoutFeedback&gt;
  24. );
  25. function SignUpForm2() {
  26. const s_context = useContext(SignUpContext);
  27. async function FinishBtnHandler()
  28. {
  29. console.log(&quot;Creating New User&quot;);
  30. console.log(&quot;Email: &quot; + emailText.trim());
  31. console.log(&quot;Password: &quot; + passwordText.trim());
  32. await createNewUser(emailText.trim(), passwordText.trim());
  33. }
  34. const navigation = useNavigation();
  35. const hobbiesData = [&quot;Football&quot;, &quot;Cricket&quot;, &quot;Programmming&quot;, &quot;Coding&quot;];
  36. const [emailText, setEmailText] = useState(&quot;&quot;);
  37. function handleEmailText(newText) {
  38. console.log(&quot;Email: &quot; + newText);
  39. setEmailText(newText);
  40. }
  41. const [passwordText, setPasswordText] = useState(&quot;&quot;);
  42. function handlePasswordText(newText) {
  43. console.log(&quot;Password: &quot; + newText);
  44. setPasswordText(newText);
  45. }
  46. function avatarPressHandler() {
  47. console.log(&quot;Pressed!&quot;);
  48. }
  49. const statusBarHeight = Platform.OS === &quot;ios&quot; ? 50 : StatusBar.currentHeight;
  50. return (
  51. &lt;KeyboardAvoidingView behavior=&quot;padding&quot; style={{ flex: 1 }}&gt;
  52. &lt;HideKeyboard&gt;
  53. &lt;ScrollView&gt;
  54. &lt;View
  55. style={{
  56. position: &quot;absolute&quot;,
  57. top: 0,
  58. left: 0,
  59. right: 0,
  60. zIndex: 999,
  61. }}
  62. &gt;
  63. &lt;View
  64. style={{
  65. backgroundColor: &quot;#f1be63&quot;,
  66. height: statusBarHeight,
  67. }}
  68. &gt;
  69. &lt;StatusBar barStyle=&quot;dark-content&quot; /&gt;
  70. &lt;/View&gt;
  71. &lt;/View&gt;
  72. &lt;View style={[styles.rootContainer, { paddingBottom: 48 }]}&gt;
  73. &lt;View style={styles.lowerContainer}&gt;
  74. &lt;Text style={styles.primaryText}&gt;You&#39;re almost there!&lt;/Text&gt;
  75. &lt;Avatar
  76. rounded
  77. size={170}
  78. containerStyle={{ alignSelf: &quot;center&quot;, marginTop: 24 }}
  79. //icon={{ name: &quot;user&quot;, type: &quot;font-awesome&quot; }}
  80. overlayContainerStyle={{ backgroundColor: &quot;#f1be63&quot; }}
  81. source={{
  82. uri: &quot;https://cdn.pixabay.com/photo/2019/11/03/20/11/portrait-4599553__340.jpg&quot;,
  83. }}
  84. &gt;
  85. &lt;Avatar.Accessory size={20} onPress={avatarPressHandler} /&gt;
  86. &lt;/Avatar&gt;
  87. &lt;Text
  88. style={[
  89. styles.secondaryText,
  90. { marginBottom: 8, marginTop: 16 },
  91. ]}
  92. &gt;
  93. Express yourself &amp; customize your avatar
  94. &lt;/Text&gt;
  95. &lt;Input
  96. isLabel={true}
  97. label=&quot;Student Email&quot;
  98. placeholder=&quot;cce22rnu@uea.ac.uk&quot;
  99. onChangeText={handleEmailText}
  100. defaultValue={emailText}
  101. /&gt;
  102. &lt;Input
  103. isLabel={true}
  104. label=&quot;Create Password&quot;
  105. placeholder=&quot;iLoveyoushakila123&quot;
  106. onChangeText={handlePasswordText}
  107. defaultValue={passwordText}
  108. /&gt;
  109. &lt;Input
  110. isLabel={true}
  111. label=&quot;Confirm Password&quot;
  112. placeholder=&quot;iLoveyoushakila123&quot;
  113. /&gt;
  114. &lt;Text style={styles.secondaryText}&gt;
  115. Now the exciting part - select your top 5 hobbies
  116. &lt;/Text&gt;
  117. &lt;View style={{ alignItems: &quot;center&quot;, marginTop: 16 }}&gt;
  118. &lt;DropDown
  119. data={hobbiesData}
  120. onSelect={(selectedItem, index) =&gt; {
  121. s_context.updateFormData(&quot;hobby_1&quot;, selectedItem);
  122. }}
  123. /&gt;
  124. &lt;DropDown data={hobbiesData} onSelect={(selectedItem, index) =&gt; {
  125. s_context.updateFormData(&quot;hobby_2&quot;, selectedItem);
  126. }}/&gt;
  127. &lt;DropDown data={hobbiesData} onSelect={(selectedItem, index) =&gt; {
  128. s_context.updateFormData(&quot;hobby_3&quot;, selectedItem);
  129. }}/&gt;
  130. &lt;DropDown data={hobbiesData} onSelect={(selectedItem, index) =&gt; {
  131. s_context.updateFormData(&quot;hobby_4&quot;, selectedItem);
  132. }}/&gt;
  133. &lt;DropDown data={hobbiesData} onSelect={(selectedItem, index) =&gt; {
  134. s_context.updateFormData(&quot;hobby_5&quot;, selectedItem);
  135. }}/&gt;
  136. &lt;CustomButton
  137. isBorder={true}
  138. title=&quot;Finish&quot;
  139. /*onPress={()=&gt;{navigation.navigate(&quot;ConfirmId&quot;)}}*/ onPress={
  140. FinishBtnHandler
  141. }
  142. /&gt;
  143. &lt;/View&gt;
  144. &lt;/View&gt;
  145. &lt;/View&gt;
  146. &lt;/ScrollView&gt;
  147. &lt;/HideKeyboard&gt;
  148. &lt;/KeyboardAvoidingView&gt;
  149. );
  150. }
  151. const styles = StyleSheet.create({
  152. rootContainer: {
  153. flex: 1,
  154. justifyContent: &quot;flex-start&quot;,
  155. alignItems: &quot;center&quot;,
  156. backgroundColor: &quot;#f1be63&quot;,
  157. marginTop: 48,
  158. backgroundColor: &quot;#f1be63&quot;,
  159. },
  160. lowerContainer: {
  161. flex: 1,
  162. width: &quot;100%&quot;,
  163. alignItems: &quot;center&quot;,
  164. },
  165. primaryText: {
  166. color: &quot;white&quot;,
  167. fontSize: 24,
  168. fontWeight: &quot;bold&quot;,
  169. },
  170. secondaryText: {
  171. marginTop: 8,
  172. color: &quot;white&quot;,
  173. fontSize: 12,
  174. fontWeight: &quot;bold&quot;,
  175. },
  176. });
  177. export default SignUpForm2;

React Context is not updating Data.

答案1

得分: 3

只返回翻译好的部分:

  1. You need to pass your userDetails state variable to the context, not the initial state value.
    你需要将userDetails状态变量传递给上下文,而不是初始的state值。

  2. Passing the initial state object will not see any updates you make.
    传递初始状态对象将不会看到你所做的任何更新。

  3. Also, you should use the functional updates format to dynamically set properties in your state.
    另外,你应该使用函数更新格式来动态设置状态中的属性。

  4. setDetails((prev) => ({ ...prev, [field]: value }));

  5. Finally, console logging state is a pointless exercise that often gives unexpected results. Just don't do it.
    最后,记录状态到控制台是一个毫无意义的操作,通常会导致意外的结果。不要这样做。

英文:

You need to pass your userDetails state variable to the context, not the initial state value

  1. &lt;SignUpContext.Provider value={{ state: userDetails, updateFormData }}&gt;
  2. {props.children}
  3. &lt;/SignUpContext.Provider&gt;

Passing the initial state object will not see any updates you make.

Also, you should use the functional updates format to dynamically set properties in your state

  1. setDetails((prev) =&gt; ({ ...prev, [field]: value }));

Finally, console logging state is a pointless exercise that often gives unexpected results. Just don't do it

huangapple
  • 本文由 发表于 2023年2月14日 06:35:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441833.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定