简体   繁体   English

addComponentAsRefTo(…):只有ReactOwner可以拥有refs-选择列表形式的错误

[英]addComponentAsRefTo(…): Only a ReactOwner can have refs - Error in Form with Select List

I have a Select list that I have built in React and it works fine but I keep getting errors from React. 我有一个内置在React中的Select列表,它可以正常工作,但是我不断从React中获取错误。 I get more errors in Minified mode which is even worse. 我在缩小模式下收到了更多错误,甚至更糟。 I have been directed to a Facebook link at https://facebook.github.io/react/docs/error-decoder.html?invariant=119 and https://facebook.github.io/react/docs/error-decoder.html?invariant=120 but none of the options there have really helped me. 我被定向到https://facebook.github.io/react/docs/error-decoder.html?invariant=119https://facebook.github.io/react/docs/error-decoder上的Facebook链接.html?invariant = 120,但那里的所有选项都没有真正帮助过我。

I have looked through my referenced libraries and can find no duplicate loading of React (It is CDN loaded on the page and externalised by Browserify / Browserify-shim - I have gone through the bundles looking for the React code and haven't found it.) I double checked by commenting out React on my page and everything dutifully fell over (as expected), so I am down to it being related to my code somehow. 我浏览了我引用的库,找不到重复的React加载(它是CDN加载到页面上,并由Browserify / Browserify-shim外部化-我遍历了所有捆绑包,寻找React代码,但没有找到它。 )我通过在页面上注释掉React进行了仔细检查,然后一切都按照预期完成了,所以我认为它与我的代码相关。

The root component looks like this... 根组件看起来像这样...

export default class Select extends React.Component {

    constructor(props) {
        super(props)

        this.state = { 
            value: this.props.value || ""
        }

        this.handleSelectChange = this.handleSelectChange.bind(this)
        this.clearValue = this.clearValue.bind(this)
    }

    handleSelectChange(e) {
        this.state.value = e.target.value
        this.props.onChange(e)
    }

    clearValue() {
        this.setState({ value: this.props.defaultValue })
        this.forceUpdate()
    }

    render() {
        const optionNodes = this.props.options.map((obj, index) => {
            return <ListItem
                key = {index.toString()}
                data = {obj}
                currentSelectedValue = {this.state.value} />
        })
        return (
            <select 
                className = "form-control drop-down" 
                id = {this.props.name}
                name = {this.props.name} 
                onChange = {this.handleSelectChange} 
                disabled = {this.props.disabled}
                value = {this.state.value}>
                {this.state.value == this.props.defaultValue && 
                    <option 
                        id = {`${this.props.name}-placeholder`} 
                        className = "placeholder" 
                        value = {this.props.defaultValue} disabled hidden>
                        {this.props.placeholder}
                    </option>}
                {optionNodes}
            </select>
        )
    }
}

This is used by a higher component here... 在此由更高的组件使用...

export default class SelectFormLine extends React.Component {

    constructor(props) {
        super(props)

        this.clearValue = this.clearValue.bind(this)
    }

    clearValue() {
        this.refs.selectList.clearValue()
    }

    render() {
        return (
            <div className="row padded-row">
                <div className="drop-down-line form-group">
                    <FormLabel
                        name = {this.props.name}
                        className = {this.props.labelClassName} 
                        value = {this.props.label} />
                    <div className={this.props.inputClassName}>
                        <Select
                            name = {this.props.name} 
                            ref = "selectList"
                            placeholder = {this.props.placeholder}
                            onChange = {this.props.onChange}
                            value = {this.props.value}
                            defaultValue = {this.props.defaultValue}
                            disabled = {this.props.disabled}
                            options = {this.props.options} />
                        <FormInputNotes>{this.props.children}</FormInputNotes>
                    </div>
                </div>
            </div>
        )
    }
}

Which sits inside my form here (snippet)... 在我的表单里面(片段)...

export default class UserManagementForm extends React.Component { 导出默认类UserManagementForm扩展了React.Component {

constructor(props) {
    super(props)

    this.state = { 
        organisationsDisabled: true,
        typesLookupData: [],
        organisationsLookupData: []
    }

    this.handleTypeChange = this.handleTypeChange.bind(this)
}

handleTypeChange(e) {
    const changedUser = this.props.object
    changedUser.role = e.target.value
    this.refs.organisationIdSelect.clearValue()
    changedUser.organisationId = ""
    this.setState({ organisationsDisabled: false })
    this.props.onChange(changedUser)
    this.loadOrganisationsLookupData(changedUser.role)
}

render() {
    const labelClassNames = "col-md-2 col-sm-4"
    const inputClassNames = "col-md-10 col-sm-8"
    return (
        <InlineContent useFor = {this.props.useFor}>
            <Form.Wrapper
                formId = {this.props.formId}
                object = {this.props.object}
                className = "col-md-12"
                onSubmit = {this.props.onSubmit}
                onSubmitSuccess = {this.props.onSubmitSuccess}>
                {this.props.object.id !== undefined && <Form.Hidden name = "ID" value = {this.props.object.id} />}
                <Form.ErrorDisplay />
                {this.props.managed && this.props.useFor == "create" && <Form.SelectLine
                    name = "OrganisationId"
                    ref = "organisationIdSelect"
                    label = "Organisation"
                    labelClassName = {labelClassNames} 
                    inputClassName = {inputClassNames} 
                    placeholder = "Please select an organisation&hellip;"
                    onChange = {this.handleOrganisationChange}
                    value = {this.props.object.organisationId}
                    disabled = {this.state.organisationsDisabled}
                    options = {this.state.organisationsLookupData} /> }
                {!this.props.managed || this.props.useFor == "edit" && <Form.Hidden name = "OrganisationId" value = {this.props.object.organisationId} />}
                <Form.Buttons.SubmitCancel
                    className = {`${inputClassNames} col-md-offset-2 col-sm-offset-4`} 
                    submitText = {this.props.submitText} 
                    onCancel = {this.props.onCancel} />
            </Form.Wrapper>
        </InlineContent>
    )
}

} }

This error is weird. 这个错误很奇怪。

I've gotten by it by just changing string refs to what Facebook has said to start using. 我只是通过将字符串引用更改为Facebook所说的开始使用而得到的。 https://facebook.github.io/react/docs/refs-and-the-dom.html https://facebook.github.io/react/docs/refs-and-the-dom.html

So basically you want to do 所以基本上你想做

ref={(i) => this._whateverYouWantToCallIt = i}

If that doesn't fix it, there may be a double React on your project. 如果那不能解决问题,那么您的项目上可能会有双重React。

暂无
暂无

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

相关问题 测试:`错误:永久违反:addComponentAsRefTo(...):只有ReactOwner可以具有引用。`错误 - Testing: `Error: Invariant Violation: addComponentAsRefTo(…): Only a ReactOwner can have refs.` error 未捕获的不变违反:addComponentAsRefTo(…):只有ReactOwner可以具有引用 - Uncaught Invariant Violation: addComponentAsRefTo(…): Only a ReactOwner can have refs 只有ReactOwner可以具有引用 - Only a ReactOwner can have refs React组件可以在浏览器中正确渲染,但是Jest在渲染时会测试错误:“只有ReactOwner可以具有引用” - React components render correctly in browser, but Jest test errors when rendering: “Only a ReactOwner can have refs” 只有ReactOwner可以有refs。 您可能正在向组件的render方法中未创建的组件添加ref - Only a ReactOwner can have refs. You might be adding a ref to a component that was not created inside a component's render method 仅当动态显示弹出窗口时,addComponentAsRefTo错误 - addComponentAsRefTo error only when dynamically showing a popup 错误:Function 组件不能有参考。? - Error : Function components cannot have refs.? 我有一个带有选择的创建元素的表单。 我该怎么做才能创建一次元素? - I have a form with select which creates elements. How I can do that elements creates only once? 错误:不变违反:使用react-router时addComponentAsRefTo - Error: Invariant Violation: addComponentAsRefTo when using react-router Refs在表单容器中是空的 - Refs are empty in a form container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM