this the first time to me with context in Reactjs and have this problem Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it’s defined in, or you might have mixed up default and named imports. Check the render method of App `
FeedbackContext.js
JavaScript
x
17
17
1
import React from "react";
2
const defaultValue = {
3
uid: null,
4
title: null,
5
appName: null,
6
type: null,
7
feedbackDescription: null,
8
}
9
10
const FeedbackContext = React.createContext({
11
selectedFeedback: defaultValue,
12
updateSelectedFeedback: () => {}
13
14
});
15
16
export default FeedbackContext ;
17
App.js
JavaScript
1
76
76
1
import React from "react";
2
import './App.css';
3
import MainNav from "./componets/Navbar/MainNav";
4
import firebase from './config/fbConfig';
5
import RoutersContainer from "./AppRouters/RoutersContainer";
6
import FeedbackContext from "./contextApi/FeedbackContext";
7
8
class App extends React.Component {
9
10
11
constructor(props) {
12
super(props);
13
this.state = {
14
userType: 'undefined',
15
selectedFeedback: {
16
uid: null,
17
title: null,
18
appName: null,
19
type: null,
20
feedbackDescription: null,
21
}
22
}
23
}
24
25
componentDidMount() {
26
firebase.auth().onAuthStateChanged(user => {
27
if (user) {
28
this.setState({userType: 'login'})
29
} else {
30
this.setState({userType: 'logout'})
31
}
32
})
33
}
34
35
updateSelectedFeedback = (feedback) => {
36
this.setState({selectedFeedback: feedback})
37
}
38
39
40
render() {
41
return (
42
<FeedbackContext.provider value={{
43
selectedFeedback: this.state.selectedFeedback,
44
updateSelectedFeedback: this.updateSelectedFeedback
45
}}>
46
<div className="App">
47
<MainNav userType={this.state.userType}/>
48
<RoutersContainer userType={this.state.userType}/>
49
50
51
{/*this button only on test mode*/}
52
<button onClick={() => {
53
{
54
if (this.state.userType === 'admin') {
55
this.setState({userType: "login"})
56
} else if (this.state.userType === 'login') {
57
this.setState({userType: "admin"})
58
}
59
}
60
}}
61
>
62
he is {this.state.userType}
63
</button>
64
65
</div>
66
</FeedbackContext.provider>
67
);
68
69
}
70
}
71
72
73
74
export default App
75
76
ShowButton.js
JavaScript
1
23
23
1
import React from "react";
2
import FeedbackContext from "../../contextApi/FeedbackContext";
3
class ShowButton extends React.Component {
4
5
6
render() {
7
return (
8
9
<FeedbackContext.consumer>
10
{({updateSelectedFeedback ,selectedFeedback }) => <>
11
<button onClick={updateSelectedFeedback(this.props.feedback)}>Show</button>
12
{console.log(selectedFeedback)}
13
</>}
14
</FeedbackContext.consumer>
15
16
17
18
)
19
}
20
}
21
22
export default ShowButton
23
Advertisement
Answer
It’s consumer should be capital.
JavaScript
1
17
17
1
//....
2
render() {
3
return (
4
5
<FeedbackContext.Consumer>
6
{({updateSelectedFeedback ,selectedFeedback }) => <>
7
<button onClick={updateSelectedFeedback(this.props.feedback)}>Show</button>
8
{console.log(selectedFeedback)}
9
</>}
10
</FeedbackContext.Consumer>
11
12
13
14
)
15
}
16
}
17