score:1
Accepted answer
import React from "react";
import ReactDOM from "react-dom";
import "./styles.css";
const Context = React.createContext({ message: "hi" });
class Child extends React.Component {
constructor() {
super();
console.log("Constructor: ", this.context); // undefined here
}
render() {
const message = this.context;
return <p>{message}</p>; // hello here
}
}
Child.contextType = Context;
function App() {
return (
<div className="App">
<Context.Provider value="hello">
<Child />
</Context.Provider>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Trying to print the context in the constructor returns undefined, but you should be able to use context in the render function like this.
You can run the example here: https://codesandbox.io/s/crazy-forest-89bd6?fontsize=14
score:0
Try to rewrite your child component like one of them below:
Dynamic Context
import React from 'react';
import Context from './Context'; // Context here is the name you defined as Context = React.createContext();
class Join extends React.Component {
render() {
let props = this.props;
let value = this.context;
return (
<div>
{/* Something important here */}
</div>
);
}
}
Join.contextType = Context; // Context here is the name you defined as Context = React.createContext();
export default Join;
Context.Consumer
import React from 'react';
import { Consumer } from './Context'; // the path is where your context file is
class Join extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<Consumer>
{value => (
{/* Here you can retrieve your messageThatllChange */}
<div>
{/* Something important here */}
</div>
)
</Consumer>
);
}
}
export default Join;
Source: stackoverflow.com
Related Query
- How do I use react context API to pass data from parent component to child component when using react-router in the parent component in React.js?
- How to make React.js fetch data from api as state and pass this state data from its parent to child component
- ReactJS How to pass iterated values to child component from parent component using Context API
- How to update data from child component to parent component using context api in react?
- How to pass in memory json data from parent component to child component in react js and to display that in tabular form in child component
- How to use react hooks to pass a state from child component to the parent component?
- How to use onClick event to pass the props from child component to parent component React Hooks
- How to pass data from child component to its parent in ReactJS?
- React | pass data from parent to child component
- How do i pass data up from a child web component to it's direct parent element
- How to pass input value from child to parent component react
- Passing API data from parent container to child component in React
- How to pass an Array from Child component to Parent component in react I know how to pass from parent t
- React TS - How to pass props from a parent component to a deeply nested child
- How to pass form values from child component to parent in react
- How to pass input values from a child Form component to the state of its parent component for submission with react hooks?
- React router dom passing data from parent component to child router component does not pass the props.match
- How do I send data from child component to parent component in React
- How to pass data from a child component to a parent component in React.js
- Pass data from child to parent and to another child component React Js
- How to pass data from child component to parent component in reactjs
- How to pass data from parent to child component in reactjs?
- In React How to pass object using history.push from a component to child component and read data
- How to reset child component from parent using React Context and Hooks
- How to fetch data from API and then pass it to another component in react after it is fully loaded?
- React & TypeScript how pass data from child to parent at onClick via props
- How to pass latest state to a Parent component from Child Component in react
- How to pass data from parent component to child after creation of child?
- How to pass data from child component to parent component?
- React context api, pass data from child to parent
More Query from same tag
- How to globally prefix all css rules generated by styled-components 5.3.5 with a parent class
- How to stop click event on Parent element when clicking the child element
- Redux withRouter in static method of superclass
- React - Get data before rendering pages
- React testing component simulate click does not recognise function
- --React does update the size of the containing div when it is populated by new content
- React not rendering search results from API onChange
- React How can open/close Model in class component
- Accessing array data through different components / pages
- Error: Cannot read property 'state' of undefined, struggeling to get frontend to work
- How to Control what Ace Editor Autocompleter writes to the File
- How to use a react-router Link or history.push within a gridjs cell
- findDOMNode is deprecated in StrictMode how to use refs instead
- React hooks performance and best practices
- Is it possible to submit form in React without redirect?
- Correct way to defer setting values in React following an API call?
- React - Promise won't add response data to array through useState()
- React JSX code not compiling
- how to flip state in usereducer react/ts
- the spread operator in Redux reducer
- Declaring function inside function in React
- React - how to map nested object values?
- Trouble with binding event handlers in react
- What is the proper way to pass parameters between urls using react router?
- how to put state array equal to another array using use State
- Calling a component gives Cannot read property 'map' of undefined
- Is there a quick way to automock react components
- Use the same form component for both, add and update. ReactJs
- Trouble adding authentication with React and Firebase
- Flow: cant create react component that takes a generic or an object