简体   繁体   English

用脚本填充并提交reactjs表单

[英]fill and submit a reactjs form with a script

I'm tryng to compare some differents frameworks (angularjs, flux+reactjs and emberjs) doing a minimal todo list application. 我试图比较做一个最小的待办事项清单应用程序的一些不同的框架(angularjs,flux + reactjs和emberjs)。

You can find my code here: 您可以在这里找到我的代码:

https://github.com/jurgob/todo_test (in the flux directory you can find the implementation) https://github.com/jurgob/todo_test (在flux目录中,您可以找到实现)

I would like to programmatically fill and send the form to add an item to my todo list. 我想以编程方式填写并发送表格以将项目添加到我的待办事项列表中。

this code works for angular and emberjs: 这段代码适用于angular和emberjs:

$('.addItemText').val('test'); $( 'addItemText ')VAL(' 测试')。 $('.addItemText').change(); $( 'addItemText。')变化()。 $('.addItemBtn').click(); $( 'addItemBtn。')点击()。

but not for reactjs. 但不适用于reactjs。

I've also tried with sendkeys jquery plugin ( https://github.com/dwachss/bililiteRange/blob/master/jquery.sendkeys.js ) without any success. 我也尝试过使用sendkeys jquery插件( https://github.com/dwachss/bililiteRange/blob/master/jquery.sendkeys.js ),但没有成功。

here is my flux implementation: https://github.com/jurgob/todo_test/blob/master/flux/main.js 这是我的流量实现: https : //github.com/jurgob/todo_test/blob/master/flux/main.js

as additional note: I've build a casperjs script to performe them from an headlerss browser, and using casper.sendKeys function it works for all the framework (you can find the code here https://github.com/jurgob/todo_test/blob/master/tests/maintest.js ) 作为附加说明:我已经构建了一个casperjs脚本,以便从headlerss浏览器中执行它们,并使用casper.sendKeys函数适用于所有框架(您可以在此处找到代码https://github.com/jurgob/todo_test/ blob / master / tests / maintest.js

That code should work for react, however you can ditch jQuery in this example and use refs. 该代码应该可以起作用,但是您可以在此示例中抛弃jQuery并使用引用。 More information about refs can be found here: https://facebook.github.io/react/docs/more-about-refs.html 关于裁判的更多信息可以在这里找到: https : //facebook.github.io/react/docs/more-about-refs.html

In reality though you shouldn't be setting properties and triggering events on your own, use state to set properties, and you can submit a form request via XHR rather than filling out a form and simulating a click. 实际上,尽管您不应该自己设置属性和触发事件,而应使用状态来设置属性,并且您可以通过XHR提交表单请求,而不是填写表单并模拟点击。

I also had a brief look at your code and noticed you were manipulating props directly, this is an anti-pattern, if you need to keep data in sync between different components consider either passing the components back upstream, creating a global eventemitter, or using a pattern like flux: https://facebook.github.io/react/blog/2014/05/06/flux.html 我还对代码进行了简要介绍,并注意到您正在直接操作道具,这是一种反模式,如果您需要使不同组件之间的数据保持同步,请考虑将组件传递回上游,创建全局事件发射器或使用像助焊剂这样的模式: https : //facebook.github.io/react/blog/2014/05/06/flux.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM