Reactjs 如何通过单击电子邮件中的链接来设置编辑框值(在react应用程序中)?
我是react编程新手,我不知道当用户单击电子邮件链接时如何设置网页超链接中的“传入数据” 换句话说,我收到一封有链接的电子邮件。。。我点击链接,该链接将我带到一个网页,并用数据(来自链接)填充两个编辑框 我知道如何编写后端api,以接受api格式的数据,例如在'discounters.js'api文件中,我可以这样做:Reactjs 如何通过单击电子邮件中的链接来设置编辑框值(在react应用程序中)?,reactjs,hyperlink,react-props,react-state,Reactjs,Hyperlink,React Props,React State,我是react编程新手,我不知道当用户单击电子邮件链接时如何设置网页超链接中的“传入数据” 换句话说,我收到一封有链接的电子邮件。。。我点击链接,该链接将我带到一个网页,并用数据(来自链接)填充两个编辑框 我知道如何编写后端api,以接受api格式的数据,例如在'discounters.js'api文件中,我可以这样做: router.post('/discounts/:userCode/discount/:discountCode' 但是我想做的是通过电子邮件发送此链接,让用户单击该链接,
router.post('/discounts/:userCode/discount/:discountCode'
但是我想做的是通过电子邮件发送此链接,让用户单击该链接,并用数据填充生成的页面
下面是一个示例链接
example.com/discountPage/:snvpsnan8zosfluawpcn50ycdipa6rrqlym/code/:sdfiedgmmkfzuky2f6grds
因此,用户单击链接后,REACT网页编辑框应如下所示:
给定以下反应(部分)成分:
class discountPage extends Component {
constructor() {
super();
this.state = {
userCode: '',
discountCode: ''
errors: {}
};
this.onChange = this.onChange.bind(this);
this.onSubmit = this.onSubmit.bind(this);
}
onChange(e) {
this.setState({ [e.target.name]: e.target.value });
}
我想我可能可以在componentDidMount中设置数据,但真的不确定链接中的数据来自何处以及如何设置
这是道具带来的吗
componentDidMount() {
console.log('Register : componentDidMount ');
this.setState({'userCode': this.props.userCode });
this.setState({'discountCode': this.props.discountCode });
}
更新
根据Pinaka30的反应。。。我搜索了一下,发现了这个:
可以有很多方法,我列出的方法可能不是最好的,但它肯定会给您提供所需的结果。
由于您正在单击一个链接,然后被重定向到表单的新页面,因此该请求将是GET请求。我们知道,在GET请求的情况下,参数在url中是可见的。因此,您可以从url本身检索用户代码和折扣代码
componentDidMount(){
var url = window.location.href; //Gives you the complete url
temp = url.split(""); //Split the url to retrieve both parameters according to your format
this.setState({
userCode: temp[0],
discountCode: temp[1]
});
}
在渲染方法中,使用状态作为值
<input id="userCode" value={this.state.userCode} />
<input id="discountCode" value={this.state.discountCode} />
因此,一旦页面加载并呈现组件,就会调用componentDidMount并更新将反映在输入框中的状态。只需小心地拆分url。我没有完全写出那一行,因为我不知道格式 正在从某个电子邮件中单击该链接。。。这有区别吗?或者不管来源如何,URL都是相同的吗?URL将保持不变:谢谢。。现在如何设置管线以接受参数?我有这样一个:但这并不是拾取参数???链接如下:在验证组件中使用
this.props.match.params.id
。在此处了解有关react路由器路径的更多信息:和
<input id="userCode" value={this.state.userCode} />
<input id="discountCode" value={this.state.discountCode} />