- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有堆栈的测试环境:
当我尝试渲染组件时,我遇到了这个恼人的错误:
PhantomJS 2.0.0 (Linux 0.0.0) NewSession should send the query after clicking on the button FAILED
Error: Invariant Violation: addComponentAsRefTo(...): Only a ReactOwner can have refs. This usually means that you're trying to add a ref to a component that doesn't have an owner (that is, was not created inside of another component's `render` method). Try rendering this component inside of a new top-level component which will hold the ref. in /tmp/35ffb917aab483a567d1be6fed779291.browserify (line 38759)
这是经过测试的文件
React = require('react')
ReactBootstrap = require('react-bootstrap')
Row = ReactBootstrap.Row
Col = ReactBootstrap.Col
Input = ReactBootstrap.Input
Button = ReactBootstrap.Button
WebUtils = require('../../web_utils.coffee')
SessionActions = require('../../actions/session_actions.coffee')
ErrorMessage = require('../error_message.coffee')
module.exports = React.createClass
contextTypes: router: React.PropTypes.func
getInitialState: ->
{ error_text: '' }
handleSubmit: (e) ->
e.preventDefault()
WebUtils.query
path: 'sessions'
data: @getFormData()
type: 'POST'
callback: ((result) ->
if result.status == 'success'
SessionActions.create(result)
@context.router.transitionTo('/')
else
@setState error_text: 'Неправильные логин и/или пароль'
).bind(this)
getFormData: ->
{
login: @refs.login.getValue(),
password: @refs.password.getValue()
}
render: ->
<form onSubmit={@handleSubmit}>
<Row>
<Col md={6} mdOffset={3} className='text-center'>
<h3>Войти в систему</h3>
{ <ErrorMessage text={@state.error_text} /> if !!@state.error_text }
<Input type='text' placeholder='Введите Ваш логин' ref='login' autoFocus />
<Input type='password' placeholder='Пароль' ref='password' />
<Button type='submit' bsStyle='success'>Вход</Button>
</Col>
</Row>
</form>
有测试:
React = require('react/react-with-addons.js')
ReactTestUtils = React.addons.TestUtils
NewSession = require('../../../../app/coffee/components/sessions/new.coffee')
describe 'NewSession', ->
it 'should send the query after clicking on the button', ->
ReactTestUtils.renderIntoDocument(<NewSession />) # here comes the error
错误消息来源:
React = require('react')
ReactBootstrap = require('react-bootstrap')
Alert = ReactBootstrap.Alert
module.exports = React.createClass
render: ->
<Alert bsStyle='danger' bsSize='small' className='text-left'>{@props.text}</Alert>
如何避免此错误?
最佳答案
如果您正在使用 webpack.conf,则添加以下 block 即可解决您的问题
externals: [{
'react': {
root: 'React',
commonjs2: 'react',
commonjs: 'react',
amd: 'react'
}
}, {
'react-dom': {
root: 'ReactDOM',
commonjs2: 'react-dom',
commonjs: 'react-dom',
amd: 'react-dom'
}
}]
关于javascript - 测试: `Error: Invariant Violation: addComponentAsRefTo(...): Only a ReactOwner can have refs.` error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32416138/
我有一个简单的 react 组件,其中包含一个表单: var AddAppts = React.createClass({ handleClick: function() {
我正在尝试使用react-data-grid ( http://adazzle.github.io/react-data-grid/index.html ),但我不断收到错误: invariant.j
我有一个在 React 中内置的选择列表,它工作正常,但我不断从 React 中收到错误。我在缩小模式下遇到更多错误,情况更糟。我被定向到 Facebook 链接 https://facebook.g
我已经编写了一个独立的组件,如果我将此组件作为独立组件运行,它可以正常工作。我想将此组件作为 NPM 模块发布,以便我可以共享此组件。在我的示例应用程序中,我在我的 package.json 中定义了
我有一个带有堆栈的测试环境: CoffeeScript react (+jsx) 业力 幻影 Jasmine 当我尝试渲染组件时,我遇到了这个恼人的错误: PhantomJS 2.0.0 (Linux
我在 React 中有两个组件可以很好地呈现并在浏览器中产生预期的行为,但在通过 Jest 运行测试时似乎无法呈现。 描述.js var React = require('react/addons')
我想获得漂亮的“基本”(美国)示例 react-magic-move在 JSFiddle 工作的组件,用于修补。 (另请参阅令人印象深刻的视频演示 https://www.youtube.com/wa
我是一名优秀的程序员,十分优秀!