score:8
It's hard to say what's going wrong without seeing a full code example, but what you're trying to do is certainly possible. Here's a working example.
class Parent extends React.Component {
constructor(props) {
super(props)
// Bind the this context to the handler function
this.handler = this.handler.bind(this);
// Set some state
this.state = {
messageShown: false
};
}
// This method will be sent to the child component
handler(id) {
this.setState({
messageShown: true,
id: id
});
}
// Render the child component and set the action property with the handler as value
render() {
console.log(this.state);
return (
<div>
<Child action={this.handler} />
<div>{this.state.id}</div>
</div>
);
}
}
class Child extends React.Component {
render() {
return (
<div>
{/* The button will execute the handler function set by the parent component */}
<button onClick={() => this.props.action(1)} > button </button>
</div>
)
}
}
ReactDOM.render(<Parent />, document.getElementById('main'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="main"></div>
score:0
Usually when this.state is undefined after invoking a callback function it is a binding issue. Double check that the handler function has this bound to it in the parent component's constructor.
this.handler = this.handler.bind(this);
More on binding: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind
score:2
To achieve what you want, in your Child component you should call a function that calls passed function. In this case you’ll be able to pass any parameter you want.
Let’s code!
Your Parent component will be:
class Parent extends React.Component {
constructor(props) {
super(props)
// Bind the this context to the handler function
this.handler = this.handler.bind(this);
// Set some state
this.state = {
messageShown: false,
id: -1 // initialize new state property with a value
};
}
// This method will be sent to the child component
handler(id) {
this.setState({
messageShown: true,
id: id
});
}
// Render the child component and set the action property with the handler as value
render() {
return <Child action={this.handler} />
}
}
And your Child component will be
class Child extends React.Component {
render() {
return (
<div>
{/* The button will execute the handler function set by the parent component, passing any parameter */}
<Button onClick={() => this.props.action(1)} />
</div>
)
}
}
Hope this helps
Source: stackoverflow.com
Related Query
- How to update parent state from child component in React + send a paramater
- How to update state of child component when it get called in map function from parent component React JS
- How to update the correct state value in a parent component that gets its value from a child component using hooks in react?
- How to pass input values from a child Form component to the state of its parent component for submission with react hooks?
- Update parent component State from child component in react js
- How to send state from Child component to another component in React
- How do I send data from child component to parent component in React
- How to update redux state using a react variable passed up to the component from a child
- update react child component's state using props received from an async api call made in the parent component
- React update child component state from parent using getDerivedStateFromProps
- How to pass latest state to a Parent component from Child Component in react
- How to update state of parent from child component in reactJS
- Not sure how to update parent state from the child component
- React update child state from parent component
- How to manipulate state of parent component from child in react
- How to update state if I have props coming from parent component in React Hooks
- How can I update state twice in a Parent component from a Child component?
- How to properly pass an array from a parent React class component state to a child component written with hooks?
- React - I am passing state from a child to a parent component but the state value is off by one update cycle
- How to pass a state from Child component to Parent Component in react
- update state of child component from parent component using react hooks
- how to send data from child component to parent component state in reactjs
- How to Set a state of parent component from child component in react js
- How to set the state of parent component in react from inside child component
- How do I access multiple state in parent from child component in React Native
- How can I update my child component state from the parent class in React?
- How to set state in parent from one child component and access the state in another child component using react and typescript?
- React JS : How To Access Child Component's State Objects From Parent Component
- Update the state from child to parent component in React
- how to stop re-rendering of child component if parent update the context or state in react js?
More Query from same tag
- React - reset state to empty array if more than 2 items selected
- Error with docker and react, digital envelope routines::unsupported
- Using Redux with models
- Post Request with axios giving error 400. u
- React-Router ^2.4.0 ERROR. Relatives routes don't find elements
- X axis by Month in Recharts
- Using wavesurfer.js in a react app cause issues
- Confirmation for custom action in Material Tabel [React]
- Mapping arrays reactjs
- How to specify null prop type in ReactJS?
- How to access object property by variable in Javascript
- MVVM architectural pattern for a ReactJS application
- How can I allow just one input selection in react?
- How to get chunks of data from array
- REACT: You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports
- how to validate dynamically added controls in react js
- How to render the checked or unchecked checkbox based on some boolean value from an api in react?
- Getting a Certificate error when making a GET request in a VS Code extension webview
- React-loadable gets chunks only from relative path
- Node Express setting cookies
- how to render header, footer, menu, only once and only for private routes with react js?
- Trying to get props from map() ES6 and pass to other component
- How To Navigate To Another Functional Component and Pass a Value in ReactJs
- Responsive Carousel React material-ui
- How can I take a group of nested JSON objects and append their data to an editable text field?
- ”Wasted time" shown by React.addons.Perf.printWasted
- Load more data from API when user scrolls the the page. REACT
- React - State set inside useEffect axios request is empty
- React + Alt: Lifecycle Gap
- getting error :Type '[null, Dispatch<SetStateAction<null>>]' is not assignable to type in typescript react