-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontextLang.js
More file actions
139 lines (109 loc) · 4.49 KB
/
contextLang.js
File metadata and controls
139 lines (109 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<<<<<<< HEAD
/*
React Context API
We provided some simple React template code. Your goal is to modify the application so that when you click the toggle button,
the favorite programming language toggles between the items in the languages array. The default value should be the first item in the array.
You must use the Context API for this challenge, which means you have to use the React.createContext and Context.Provider functions.
You are free to add classes and styles, but make sure you leave the component ID's and classes provided as they are.
Submit your code once it is complete and our system will validate your output.
*/
import React, { useState, createContext, useContext } from 'react';
import { createRoot } from 'react-dom/client';
// Array of languages
const languages = ['JavaScript', 'Python'];
// 1. Create a Context to hold the language and the toggle function
const LanguageContext = createContext();
// 2. Create a Context Provider to provide the state and toggle function
function LanguageProvider({ children }) {
const [favoriteLanguage, setFavoriteLanguage] = useState(languages[0]);
const toggleLanguage = () => {
setFavoriteLanguage(prev => {
const currentIndex = languages.indexOf(prev);
const nextIndex = (currentIndex + 1) % languages.length;
return languages[nextIndex];
});
};
return (
<LanguageContext.Provider value={{ favoriteLanguage, toggleLanguage }}>
{children}
</LanguageContext.Provider>
);
}
function App() {
// Wrap the MainSection with the LanguageProvider to provide the context
return (
<LanguageProvider>
<MainSection />
</LanguageProvider>
);
}
function MainSection() {
// Use the context to get the current favorite language and toggle function
const { favoriteLanguage, toggleLanguage } = useContext(LanguageContext);
return (
<div>
{/* Display the current favorite language */}
<p id="favoriteLanguage">favorite programming language: {favoriteLanguage}</p>
{/* Button to toggle between languages */}
<button id="changeFavorite" onClick={toggleLanguage}>toggle language</button>
</div>
);
}
const container = document.getElementById('root');
const root = createRoot(container);
root.render(<App />);
=======
/*
React Context API
We provided some simple React template code. Your goal is to modify the application so that when you click the toggle button,
the favorite programming language toggles between the items in the languages array. The default value should be the first item in the array.
You must use the Context API for this challenge, which means you have to use the React.createContext and Context.Provider functions.
You are free to add classes and styles, but make sure you leave the component ID's and classes provided as they are.
Submit your code once it is complete and our system will validate your output.
*/
import React, { useState, createContext, useContext } from 'react';
import { createRoot } from 'react-dom/client';
// Array of languages
const languages = ['JavaScript', 'Python'];
// 1. Create a Context to hold the language and the toggle function
const LanguageContext = createContext();
// 2. Create a Context Provider to provide the state and toggle function
function LanguageProvider({ children }) {
const [favoriteLanguage, setFavoriteLanguage] = useState(languages[0]);
const toggleLanguage = () => {
setFavoriteLanguage(prev => {
const currentIndex = languages.indexOf(prev);
const nextIndex = (currentIndex + 1) % languages.length;
return languages[nextIndex];
});
};
return (
<LanguageContext.Provider value={{ favoriteLanguage, toggleLanguage }}>
{children}
</LanguageContext.Provider>
);
}
function App() {
// Wrap the MainSection with the LanguageProvider to provide the context
return (
<LanguageProvider>
<MainSection />
</LanguageProvider>
);
}
function MainSection() {
// Use the context to get the current favorite language and toggle function
const { favoriteLanguage, toggleLanguage } = useContext(LanguageContext);
return (
<div>
{/* Display the current favorite language */}
<p id="favoriteLanguage">favorite programming language: {favoriteLanguage}</p>
{/* Button to toggle between languages */}
<button id="changeFavorite" onClick={toggleLanguage}>toggle language</button>
</div>
);
}
const container = document.getElementById('root');
const root = createRoot(container);
root.render(<App />);
>>>>>>> 7831ccac1e123cb37a7ad9caf4fac1653333ed5b