How to customize how to transfer data to < Route/ > in react-router-dom

how do I pass data between components using the < Route/ > tag of react-router-dom ? (without using state machines such as redux)

data can be transmitted in this way when it is normal between the two components

getDataFromSub = (data) =>{
    // 
    console.log(data)
}

<div>
    
    <div>
        <SubComponent setDataToParent={this.getDataFromSub}/>
    </div>
</div>
state={
    data:[
        {id:1,value:"aaa"},
        {id:2,value:"bbb"},
        {id:3,value:"ccc"},
    ]
}

//
this.props.setDataToParent(this.state.data)

<div>
    
</div>

but you can"t pass data like this after using the Route tag

<div>
    
    <div>
        //  
        <Route path={"/get_data"} component={SubComponent} setDataToParent={this.getDataFromSub} />
    </div>
</div>
Dec.30,2021

  1. Url parameters : define a route in the form of /: id . You can use match.params.id to get
  2. .
  3. Query parameters : add parameters to the route, such as / account?name=netflix , and get it yourself
  4. history.push (path, [state]) : push the second parameter can be transferred to any object, and it should be taken out in location.state
  5. .

you can use the query state method of router to pass parameters. It is important to note that query state will be lost after browser refresh

Menu