Ad
React: Creating A Dynamic Route Based On A Prop
I'm retrieving some data from a server, which i map into subcomponents in my react project.
componentDidMount() {
axios.get('http://localhost:5000/machinelearning')
.then(res => this.setState({projects: res.data, isLoading: false}))
}
...
const projects = this.state.isLoading ? <CircularProgress color="secondary" size={200}/> : this.state.projects.projects[0].map((project, i) =>{
const projectObj = Object.assign({}, project)
console.log(projectObj)
return(
<div style={{padding: '15px'}} key={i+1}>
<MachineLearningProject project={projectObj} />
</div>
within each component, i want to make a dynamic route, that links to the a component of the same name, passed in from the server.
I make a dynamic Link on a picture on my component.
<Link to={`${props.project.name}`} >
<CardMedia
className={classes.media}
image={`data:image/jpeg;base64,${props.project.picture}`}
title="Picture"
/>
</Link>
with a dynamic Route in the bottom of the component
<Route path="/:project"
component={ChatBot} //this component is hardcoded for testing purposes
/>
ideally i want it to look like this
but it does not like anywhere, what am i doing wrong?
Ad
Answer
You need to update dynamic Route of component to
<LinkContainer to={props.project.name}>
<Button> Label Stuff </Button>
</LinkContainer>
Ideally, your App.js
should discuss which component to load based on link as Router logic resides there.
<Router>
<Switch>
<Route path={"Your Path"} component={"You Component"} />
</Switch>
<Router>
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad