I am new to react js
. This might be an easy question but I am not getting a way to do this. So, I am asking this question.
I have a grandParentComp which is like
GrandParent
import React from 'react';
class QuizSetupMain extends React.Component {
constructor(props) {
super(props);
this.state = {
technologies: this.props.technologies,
hasError: false
}
}
render() {
return (
<div >
{this.state.technologies.length > 0 ? <LowLevelCriteria techData={this.state.technologies} /> : null}
<div className="fetchBtnDiv mx-auto">
<button className="btn btn-primary fetchBtnSize" disabled={this.state.hasError}>Fetch Questions</button>
</div>
</div>
Herem, I have button which I want to disable on the basis of the state variable
LowCriteria.js this is the child
import React from 'react';
class LowLevelCriteria extends React.Component {
constructor(props) {
super(props);
this.state = {
technologies: this.props.techData,
lowData: this.props.lowData.low,
hasError: false
}
}
componentWillReceiveProps(nextProps) {
if (nextProps.lowData.Low.length > 0) {
console.log("in this");
var previous_data = nextProps.lowData;
var i = 0;
previous_data.Low.map((object) => {
i = i + parseInt(object.numberOfQuestions);
});
console.log("value of i is ==>", i);
if (i >= 6) {
this.setState({
hasError: true
})
} else {
this.setState({
hasError: false
})
}
}
}
render() {
return (
<div>
<div className="questionLevelIndication">
<span className="levelIndicatorBtn backgroundColorForLow">
1
</span>
<label className="levelIndicationLabel">
Low Difficulty Level - Maximum 6 questions
</label>
{this.state.hasError && <div class="alert alert-danger alert-dismissible fade show">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Please reduce number of questions</strong>
</div>}
</div>
{(this.props.lowData) && this.props.lowData.Low.length > 0 && this.props.lowData.Low.map(data => (
<LowRow technologies={this.state.technologies} hasError={this.state.hasError} onChange={this.onchange.bind(this)} data={data} key={data.id} onAddRow={this.onaddRow.bind(this)} onRemoveRow={this.onRemoveRow.bind(this)} />
))}
</div>
)
}
So, Now I want to pass this this.state.hasError
to the parent , so that I can disable the button which is on the parent component.
I dont want to use the redux.
Can any one help me with this ?
I don't have any button or function that is going to get called from the child function .so that I can update the parent state.