【发布时间】:2017-07-18 11:09:59
【问题描述】:
我正在尝试将这个 conversational-form npm module 导入到我的 Create-React-App 应用程序中,但在任何尝试这样做时都会遇到编译错误。
这是确切的错误:
./~/conversational-form/dist/conversational-form.js
Module not found: Can't resolve 'conversationalform' in '/Users/user/Desktop/app_name/node_modules/conversational-form/dist'
到目前为止,我已经尝试像这样导入它
import ConversationalForm from 'conversational-form';
或使用“dist”文件夹中的js文件的绝对路径,甚至只是将那个js文件包含在我的项目中,但总是会出现编译错误。
模块is supposed to work with React。
任何想法可能导致这种情况?也许 Create-React-App 与此不兼容?我应该只包含该模块中的 JQuery 插件吗?谢谢。
更新:模块现在似乎已修复,但在尝试将示例调整为 Webpack/React/Component 结构时仍然出现错误
错误:
TypeError: Cannot read property 'setAttribute' of undefined
componentDidMount(){
13 | // add Conversational Form info
> 14 | this.refs.name.setAttribute('cf-questions', "Your name?");
15 | this.refs.email.setAttribute('cf-questions', "Your email?");
16 | this.refs.description.setAttribute('cf-questions', "What is description?");
代码
import React, { Component } from 'react';
import { CSSTransitionGroup } from 'react-transition-group';
import ConversationalForm from './conversational_form';
class ConversationForm extends Component {
constructor(props) {
super(props);
this.cf = null; // <-- Conversational Form ref
}
componentDidMount(){
// add Conversational Form info
this.refs.name.setAttribute('cf-questions', "Your name?");
this.refs.email.setAttribute('cf-questions', "Your email?");
this.refs.description.setAttribute('cf-questions', "What is description?");
this.cf = window.cf.ConversationalForm.startTheConversation({
formEl: this.refs.form,
context: document.getElementById("cf-context"),
flowStepCallback: function(dto, success, error){
success();
},
submitCallback: function(){
// this callback could also be added to the React.createElement it self...
alert("You made it!")
console.log("Form submitted...");
}
});
}
render() {
var conversation;
if (this.props.visible) {
conversation =
<div>
<form id="form" ref={form => this.input = form} className="form">
<input type="text" ref={name => this.input = name} placeholder="Name (required)" defaultValue={this.props.name} />
<input type="email" ref={email => this.input = email} placeholder="Email" defaultValue={this.props.email} />
<textarea ref={description => this.input = description} placeholder="Description" defaultValue={this.props.description} />
<button type="submit">Submit</button>
</form>
<section id="cf-context" role="cf-context" cf-context>
</section>
</div>
}
return (
<CSSTransitionGroup
transitionName="success"
transitionEnterTimeout={500}
transitionLeaveTimeout={300}>
{conversation}
</CSSTransitionGroup>
)
}
}
export default ConversationForm;
【问题讨论】:
-
你不想要一个 react-conversational-form 模块吗?
标签: reactjs npm create-react-app