简体   繁体   English

无法添加属性,在 React 组件中绑定 props 时对象不可扩展

[英]Cannot add property, object is not extensible when binding props in React component

For some reason when I try to bind a function to a prop in my React component, it is coming up with出于某种原因,当我尝试将一个函数绑定到我的 React 组件中的一个 prop 时,它出现了

TypeError: Can't add property onSearch, object is not extensible.类型错误:无法添加属性 onSearch,对象不可扩展。

I am not too familiar with what this means or why it is appearing, I think it may be to do with the es6 bindings which I am still finding my way around on.我不太熟悉这意味着什么或它为什么出现,我认为这可能与我仍在寻找解决方法的 es6 绑定有关。

Here are the two most relevant components.这是两个最相关的组件。

Searchbox搜索框

import React from 'react';

import SearchForm from 'SearchForm';
import searchDisplay from 'searchDisplay';
import googleRequests from 'googleRequests';

class SearchBox extends React.Component {
constructor() {
    super();
    this.state = {
        searchResults: []
    }
    this.handleSearch = this.handleSearch.bind(this);


}
handleSearch(searchTerm) {
    googleRequests.search(searchTerm).then((response) => {
        console.log(response.items);
        this.extrapolateResults(response.items);
    }), ((error) =>{
        console.log(error)
    });
}
//pull relevant data out of api call
extrapolateResults(arr) {
    function Book(objInArr) {
        this.link = objInArr.selfLink;
        this.bookTitle = objInArr.volumeInfo.title;
        this.author = objInArr.volumeInfo.authors;
        this.bookDescription = objInArr.volumeInfo.description;
        this.thumbnail = function() {
            if (objInArr.volumeInfo.hasOwnProperty('imageLinks')){
            return objInArr.volumeInfo.imageLinks.smallThumbnail
        } 
        else {
            return "No Thumbnail Available";
        }
    };
        this.thumbnailPic = this.thumbnail();
}
//push extrapolated data into array
var finalRes = [];
var initRes = arr;
    initRes.forEach(function (objInArr) {
        var obj = new Book(objInArr);
        finalRes.push(obj);
    })
this.setState({
    searchResults: finalRes
})
console.log(finalRes, this.state.searchResults)

}


render() {
    var res = this.state.searchResults;

    function renderResults() {
        if (res.length !== 0) {
            return (<SearchDisplay content={res} />)
        }
        else {
            return;
        }
    }

    var style = {
        border: '1px solid black',
        height: '80%',
        width: '83%'
    }
    return (
        <div style={style}>
            <SearchForm onSearch={this.handleSearch}> </SearchForm>
        </div>)
}
};

export default SearchBox;

Searchform搜索表单

import React from 'react';

class SearchForm extends React.Component {
constructor(props) {
    super(props);
    this.onFormSubmit = this.onFormSubmit.bind(this);
    this.props.onSearch = props;
}
onFormSubmit(e) {
    e.preventDefault();
    var searchTerm = this.refs.searchTerm.value;
    if (searchTerm.length > 0) {
        this.refs.searchTerm.value = '';
        this.props.onSearch(searchTerm);
    }
}

render() {
    var style = {
        border: '1px solid black',
        float: 'left',
        height: '100%',
        width: '30%'
    }

    return(
        <div style={style} className="container">
            <form onSubmit={this.onFormSubmit}>
                <input type="text" placeholder="search name here" ref="searchTerm"/>
                <input type="submit" className="button" value="Get Book"/>
            </form>
        </div>
        );
}
};

export default SearchForm;

I have a feeling I am missing something very simple but after googling this issue for a while I still can't figure out what it is...我有一种感觉我错过了一些非常简单的东西,但是在谷歌搜索这个问题一段时间后我仍然无法弄清楚它是什么......

remove this.props.onSearch = props;删除this.props.onSearch = props; ... not sure what you wanted to do there on that line ... 不确定你想在那条线上做什么

将 handleSearch 函数定义更改为 handleSearch = () => {} 胖箭头函数,它将在搜索框文件中正常工作。

暂无
暂无

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

相关问题 React:无法添加属性“X”,对象不可扩展 - React : cannot add property 'X', object is not extensible React App-TypeError:无法添加属性setState,对象不可扩展 - React App - TypeError: Cannot add property setState, object is not extensible 无法添加属性“X”,对象不可扩展 angular 9 - Cannot add property "X", object is not extensible angular 9 TypeError:无法添加属性 0,object 不可扩展 - TypeError: Cannot add property 0, object is not extensible 无法添加属性 0,尝试将类构造的对象推入数组列表时对象不可扩展 - React/NextJs - Cannot add property 0, object is not extensible when trying to push class-constructed objects indo an array list - React/NextJs 如何在React-VR中使用Animated with Shape原语(例如Box)? 错误:“无法添加_tracking属性,对象不可扩展” - How to use Animated with Shape primitives such as Box in React-VR? Error: “Cannot add property _tracking, object is not extensible” 通过道具发送对象属性时无法渲染组件 - Cannot render component when sending object property via props 材料表类型错误:无法添加属性表数据,对象不可扩展 - Material-table TypeError: Cannot add property tableData, object is not extensible Jest TestEnvironment - 类型错误:接下来无法添加属性,object 不可扩展 - Jest TestEnvironment - TypeError: Cannot add property next, object is not extensible 未捕获的类型错误:无法添加属性 0,object 在 Array.push 不可扩展 - Uncaught TypeError: Cannot add property 0, object is not extensible at Array.push
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM