ReactJS onClick arguments not getting passed to parent component
Date : March 29 2020, 07:55 AM
I wish this help you I think you should pass the function itself, not "invoke the function". Delete () here. <ProfileDetails
{...this.props.uiStore.profile}
updateNotifications={this.updateNotifications}
/>
|
How to setState in child component when child function called from parent component in ReactJS
Date : March 29 2020, 07:55 AM
will be helpful for those in need The problem seems to be that when calling setState inside the getAlert function of child, this.setState will come undefined. This happens because this inside your getAlert function doesn't refer to the context of the React Component and setState is defined for the Component. You can solve this by binding the getAlert function. You can do it in two ways. class Child extends Component {
constructor() {
super();
this.getAlert = this.getAlert.bind(this);
}
getAlert() {
alert('clicked');
//HERE I NEED TO SETSTATE
}
render() {
return (
<h1>Hello</h1>
);
}
}
getAlert = () => {
alert('clicked');
//HERE I NEED TO SETSTATE
}
class Parent extends React.Component {
render() {
return (
<div>
<Child ref={instance => { this.child = instance; }} />
<button onClick={() => { this.child.getAlert(); }}>Click</button>
</div>
);
}
}
class Child extends React.Component {
constructor(){
super();
this.state = {message:""};
}
getAlert = () => {
alert('clicked');
this.setState({message: "somemessage"});
}
render() {
return (
<div>{this.state.message!=""?(
<h1>{this.state.message}</h1>
):(
<h1>Hello</h1>
)}</div>
);
}
}
ReactDOM.render(<Parent/>, document.getElementById('app'))
<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="app"/>
|
Get current state of child component through prop passed by Parent - Reactjs
Date : March 29 2020, 07:55 AM
hope this fix your issue Parent component cannot query the state of the child component. At least, that's not the intended design of React. What I think you're asking is how to coordinate the state of child with parent, and you're on the right track to use a prop to pass the state from child to parent. class Parent extends React.Component {
state = { name: "John" }
handleChildAnimal = animal =>
this.setState({ animal });
handleClick = e =>
console.log(`Child animal: ${this.state.animal}`);
render() {
return (
<div>
<Child onAnimal={this.handleChildAnimal} />
<button onClick={this.handleClick}>Tell me Animal state</button>
</div>
);
}
}
class Child extends React.Component {
state = { animal: "Lion" }
handleClick = e => {
console.log(`Animal: ${this.state.animal}`);
this.props.onAnimal(this.state.animal);
}
render() {
return (
<button onClick={this.handleClick}>{this.state.animal}</button>
);
}
}
|
ReactJS - How can I properly render content that originates in a parent component and is passed down to a child componen
Date : March 29 2020, 07:55 AM
around this issue I can't seem to render my column names in the CarTable.tsx when the order array of strings is passed down as prop to this component. I know it's some quirky asynchronous programming. Also when I click the button, the order of column names should change and this change should be reflected in a re-render. , In CarTable constructor, remove this.setState({
cars: this.props.cars,
columnOrder: this.props.order
});
|
Child component's local state changes when change happens inside parent component : ReactJS
Date : March 29 2020, 07:55 AM
may help you . Sorry, my here below answer was wrong. In your main component, you set your state.columns just once, when the component did mount. It will never be changed after. ...
constructor(props) {
super(props);
this.state = {
data: [
{ firstName: "Jack", status: "Submitted", age: "14" },
{ firstName: "Simon", status: "Pending", age: "15" },
{ firstName: "Pete", status: "Approved", age: "16" },
{ firstName: "Lucas", status: "Rejected", age: "19" }
],
selectedValues: {},
};
}
...
buildColumns = ({ data, selectedValues = {} }) => {
const { firstName, status } = selectedValues
return [
{
Header: () => (
<div>
<div style={{ position: "absolute", marginLeft: "10px" }}>
<Child
key="firstName"
name="firstName"
options={this.getValuesFromKey(data, "firstName")}
selectedOption={firstName}
handleFilter={this.handleFilter}
/>
</div>
<span>First Name</span>
</div>
),
accessor: "firstName",
sortable: false,
show: true,
displayValue: " First Name"
},
{
Header: () => (
<div>
<div style={{ position: "absolute", marginLeft: "10px" }}>
<Child
key="status"
name="status"
options={this.getValuesFromKey(data, "status")}
selectedOption={status}
handleFilter={this.handleFilter}
/>
</div>
<span>Status</span>
</div>
),
accessor: "status",
sortable: false
},
{
Header: "Age",
accessor: "age"
}
];
}
render() {
const { data, selectedValues } = this.state;
const columns = this.buildColumns({ data, selectedValues })
return (
<div>
<ReactTable
data={data}
columns={columns}
defaultPageSize={10}
className="-striped -highlight"
/>
</div>
);
}
|