0

I have two components the Root Component that contains list of data. the data is displayed from firebase. the second component is a DateRangepicker that helps picks to dates so We can filter the root's component data.

    class Child extends React.Component {
    pickDateRange = (e: React.FormEvent<HTMLFormElement>) => {
    // getting data successufly from firebase
    // this is where I get the error trying to set the root component's props
    this.props.data = data
    }
    render() {
    return (
            <div>
                <form onSubmit={this.pickDateRange} className={classes.container} noValidate>
                    <TextField
                        id="datestart"
                        label="Starting Date"
                        type="date"
                        onChange={this.handleStartDate}                        
                    />
                    <TextField
                        id="dateend"
                        label="Ending Date"
                        type="date"
                        onChange={this.handleEndDate}
                        className={classes.textField}                        
                    />
                    <Button
                        type="submit"
                    >
                        Filter
                </Button>
                </form>                
            </div>
        );
    }}

class Parent extends React.Component {
       render() {
                 return (<div> <ChildB data={this.state.data}/></div>)
}}
Ayoub Bani
  • 189
  • 1
  • 15

1 Answers1

0

You cannot alter the props from inside the child component, either pass a callback from the parent via props to the child or use state in the child component or use your redux store.

Well the easy fix would be:

this.props.changeData(data)

with parent render:

render() {
   return (<div> <ChildB data={this.state.data} changeData={(data)=>{this.setState({data: data})}/></div>)
}

but it depends and you don't really want to set the parents state trough a child like that.

xDreamCoding
  • 1,654
  • 1
  • 12
  • 18