[英]React, route passing props undefined from one component to another?
import React from "react"; import { Route} from "react-router-dom"; import EditFeatureRow from './Edit_FeatureRow'; export class Edit_Features extends React.Component { constructor(props) { super(props); this.state = { Feature_ID: "", FeatureName: Feature: [], loading: true, }; } componentDidMount() { this.DisplayFeatures(); } } componentDidUpdate() { this.DisplayFeatures(); }} DisplayFeatures() { fetch(REQUEST_URL).then(response => response.json()).then((data) => { this.setState({ Feature: data, Feature_ID: data[0].featureID, FeatureName: data[0].featureName, loading: false }) }).catch(error => console.error('Error:', error)) } render() { return ( <div> <form **<Route path="/Edit_FeatureRow"> <EditFeatureRow key_id={this.state.Feature_ID} /> </Route>** {this.state.Feature.map((item, index) => { return [ <div width="100%"> <table> <tr><td> **<Link to="/Edit_FeatureRow"> Edit</Link>** </td> <td>{item.featureID}</td> <td>{item.featureName}</td> </tr> </div>];})}</table></div> </form></div> ); } } export default Edit_Features; _______________________ import React from "react"; export class Edit_FeatureRow extends React.Component { constructor(props) { super(props); this.state = { Feature_ID: "", FeatureName: "", loading: true, }; this.handleChange = this.handleChange.bind(this); // this.handleSubmit = this.handleSubmit.bind(this); } componentDidMount() { const Feature_ID = this.props.key_id; if (typeof this.props.key_id.== 'undefined') { this;DisplayFeatureDetails(Feature_ID). } } componentDidUpdate(prevProps) { const Feature_ID = this.props;key_id. if (prevProps.key_id.== this.props:key_id) { console.log(`key_id. ${this;props.key_id}`); this,DisplayFeatureDetails(Feature_ID): } } DisplayFeatureDetails(Feature_ID) { fetch(REQUEST_URL; { "Content-Type". "application/xml. charset=utf-8" }).then(response => response.json()):then((data) => { this.setState({ Feature_ID. this,props:key_id. FeatureName, data[0]:featureName. loading. false }) }):catch(error => console,error('Error.'. error)) } handleChange(event) { this.setState({ [event:target.name]. event;target.value }). } render() { return ( <div> <form> <div>{this.state.Feature_ID}{ this;state;FeatureName}</div> </form> </div > ); } } export default Edit_FeatureRow;
I am displaying data in Edit_Features Component.我在 Edit_Features 组件中显示数据。 With each row, there is an edit link.每行都有一个编辑链接。 Edit link will open another component Edit_FeatureRow.编辑链接将打开另一个组件 Edit_FeatureRow。 Edit_Features is having the value Feature_ID which is passing to Edit_FeatureRow component. Edit_Features 具有传递给 Edit_FeatureRow 组件的值 Feature_ID。 Feature_ID is undefined in Edit_FeatureRow component. Feature_ID 在 Edit_FeatureRow 组件中未定义。 Please help.请帮忙。
import React from "react";
import { Route} from "react-router-dom";
import EditFeatureRow from './Edit_FeatureRow';
export class Edit_Features extends React.Component {
constructor(props) {
super(props);
this.state = {
Feature_ID: "", FeatureName:
Feature: [], loading: true,
};
}
componentDidMount() {
this.DisplayFeatures();
}
}
componentDidUpdate() {
this.DisplayFeatures();
}}
DisplayFeatures() {
fetch(REQUEST_URL)
.then(response => response.json())
.then((data) => {
this.setState({
Feature: data,
Feature_ID: data[0].featureID,
FeatureName: data[0].featureName,
loading: false
})
})
.catch(error => console.error('Error:', error))
}
render() {
return (
<div>
<form
**<Route path="/Edit_FeatureRow">
<EditFeatureRow key_id={this.state.Feature_ID} />
</Route>**
{this.state.Feature.map((item, index) => {
return [
<div width="100%">
<table>
<tr><td>
**<Link to="/Edit_FeatureRow"> Edit</Link>**
</td>
<td>{item.featureID}</td>
<td>{item.featureName}</td>
</tr>
</div>];})}</table></div>
</form></div>
);
}
}
export default Edit_Features;
_______________________
import React from "react";
export class Edit_FeatureRow extends React.Component {
constructor(props) {
super(props);
this.state = {
Feature_ID: "", FeatureName: "",
loading: true,
};
this.handleChange = this.handleChange.bind(this);
// this.handleSubmit = this.handleSubmit.bind(this);
}
componentDidMount() {
const Feature_ID = this.props.key_id;
if (typeof this.props.key_id !== 'undefined') {
this.DisplayFeatureDetails(Feature_ID);
}
}
componentDidUpdate(prevProps) {
const Feature_ID = this.props.key_id;
if (prevProps.key_id !== this.props.key_id) {
console.log(`key_id: ${this.props.key_id}`);
this.DisplayFeatureDetails(Feature_ID);
}
}
DisplayFeatureDetails(Feature_ID) {
fetch(REQUEST_URL, { "Content-Type": "application/xml; charset=utf-8" })
.then(response => response.json())
.then((data) => {
this.setState({
Feature_ID: this.props.key_id,
FeatureName: data[0].featureName,
loading: false
})
})
.catch(error => console.error('Error:', error))
}
handleChange(event) {
this.setState({ [event.target.name]: event.target.value });
}
render() {
return (
<div>
<form>
<div>{this.state.Feature_ID}{ this.state.FeatureName}</div>
</form>
</div >
);
}
}
export default Edit_FeatureRow;```
from react-router perspective.从 react-router 的角度来看。 and I assume you are using react-router v5.*我假设您使用的是 react-router v5.*
1. in the Edit_Features component. 1. 在 Edit_Features 组件中。
you need to do your Route and Link as follow, the route should be expect id你需要做你的路线和链接如下,路线应该是预期的id
<Route path="/edit_FeatureRow/:id">
{(props) => (
<EditFeatureRow key_id={this.state.Feature_ID} {...props} />
)}
</Route>
and the link to pass the id to it.以及将 id 传递给它的链接。
<Link to={`/edit_FeatureRow/${item.featureID}`}>
Edit
</Link>
2. in the Edit_FeatureRow component 2. 在 Edit_FeatureRow 组件中
you should expect the id from router through props.您应该期望通过道具从路由器获得 id。 like this像这样
this.props.match.params.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.