如何在 React 中使用 props 处理参数?
How to work params using props in React?
我是新开发者 ReactJS,我在前端用 ReactJS 开发了一个 table,后端上的 NodeJS 和数据库上的 MySQL。
我想要当我点击操作列上的查看按钮时,如下所示:
我的路由器:
exports.viewclient = function(req, res) {
var Code = req.query.Code;
console.log(req.params);
connection.query('SELECT Code, Prenom, Nom, FAX, Telephone, Email, Adresse1, Adresse2 FROM clients WHERE Code = ?',[req.params.Code], function(error, results, fields) {
if (error) throw error;
res.send(JSON.stringify(results));
console.log(results);
});
}
我的服务器:
router.get('/viewclient/:Code', clients.viewclient);
我的 handleView 方法在 class Liste 上:
constructor(props) {
super(props);
this.state = {
clients: [],
Code: this.props.match.params.Code
};
handleView(Code) {
try {
console.log("Voir client")
this.props.history.push('/clients/viewclient/' + Code);
}
catch (error) {
this.setState({ error });
}
}
查看 Class :
constructor(props) {
super(props);
this.state = {
clients: [],
Code: this.props.match.params.Code
};
console.log(this.props);
componentDidMount() {
axios.get('http://localhost:4000/app/viewclient/' + this.state.Code)
.then(response => {
if (response && response.data) {
this.setState({ clients: response.data });
}
})
.catch(error => console.log(error));
}
当我运行后端用Postmanhttp://localhost:4000/app/viewclient/1111
它returns
[{"Code":1111,"Prenom":"test","Nom":"test","FAX":"58985688888","Telephone":"58985699888","Email":"test@gmail.com","Adresse1":"","Adresse2":""}]
但是当我 运行 我的前端时,它会将我重定向到 http://localhost:3000/app/viewclient/undefined
而我无法查看 Select 的结果。
console.log(this.props)的结果是:
请问我该如何解决?
我相信您可以做以下两件事中的一件来解决您的问题。
而不是使用 this.state.code
,为什么不在您的 axios 请求中简单地使用 this.props.match.params.Code
?
未定义的原因是因为在构造函数中您收到 props
,而不是 this.props
,
所以你应该只需要将它更改为以下内容。
Code: props.match.params.Code
我是新开发者 ReactJS,我在前端用 ReactJS 开发了一个 table,后端上的 NodeJS 和数据库上的 MySQL。
我想要当我点击操作列上的查看按钮时,如下所示:
我的路由器:
exports.viewclient = function(req, res) {
var Code = req.query.Code;
console.log(req.params);
connection.query('SELECT Code, Prenom, Nom, FAX, Telephone, Email, Adresse1, Adresse2 FROM clients WHERE Code = ?',[req.params.Code], function(error, results, fields) {
if (error) throw error;
res.send(JSON.stringify(results));
console.log(results);
});
}
我的服务器:
router.get('/viewclient/:Code', clients.viewclient);
我的 handleView 方法在 class Liste 上:
constructor(props) {
super(props);
this.state = {
clients: [],
Code: this.props.match.params.Code
};
handleView(Code) {
try {
console.log("Voir client")
this.props.history.push('/clients/viewclient/' + Code);
}
catch (error) {
this.setState({ error });
}
}
查看 Class :
constructor(props) {
super(props);
this.state = {
clients: [],
Code: this.props.match.params.Code
};
console.log(this.props);
componentDidMount() {
axios.get('http://localhost:4000/app/viewclient/' + this.state.Code)
.then(response => {
if (response && response.data) {
this.setState({ clients: response.data });
}
})
.catch(error => console.log(error));
}
当我运行后端用Postmanhttp://localhost:4000/app/viewclient/1111
它returns
[{"Code":1111,"Prenom":"test","Nom":"test","FAX":"58985688888","Telephone":"58985699888","Email":"test@gmail.com","Adresse1":"","Adresse2":""}]
但是当我 运行 我的前端时,它会将我重定向到 http://localhost:3000/app/viewclient/undefined
而我无法查看 Select 的结果。
console.log(this.props)的结果是:
我相信您可以做以下两件事中的一件来解决您的问题。
而不是使用
this.state.code
,为什么不在您的 axios 请求中简单地使用this.props.match.params.Code
?未定义的原因是因为在构造函数中您收到
props
,而不是this.props
, 所以你应该只需要将它更改为以下内容。
Code: props.match.params.Code