Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
226 views
in Technique[技术] by (71.8m points)

javascript - searchChange is not defined no-undef

SearchBox.js File is:

import React from 'react';

const SearchBox = ({ searchfield, searchChanger}) => {
return (
    <div className='pa2'>
        <input 
            className='pa3 b--green bg-light-blue'
            type='search'
            placeholder='search robots'
            onChange={searchChange}
        />
    </div>
);
}

export default SearchBox;

App.js File is:

import React, { Component } from 'react';
import CardList from './CardList';
import SearchBox from './SearchBox';
import { robots } from './robots';
import './App.css';
class App extends Component {
    constructor() {
        super()
        this.state = {
            robots: robots,
            searchfield:''
        }
    }

    componentDidMount() {
        fetch('https://jsonplaceholder.typicode.com/users')
            .then(response => response.json())
            .then(users => {this.setState({ robots: robots})});

    }

    onSearchChange = (event) => {
        this.setState({ searchfield: event.target.value })
    }

    render() {
        const filteredRobots = this.state.robots.filter(robots => {
           return  robots.name.toLowerCase().includes(this.state.searchfield.toLowerCase());
        })
        if (robots.length ===0) {
        return <h1>Loading</h1>
       }
        else {
          return (  
              <div className='tc'>
                    <h1 className='f1'>RoboFriends</h1>
                    <SearchBox searchChange={this.onSearchChange}/>
                    <CardList robots={filteredRobots} />
              </div>
            );
        }
    }   
}


export default App;

I was able to get rid of this error just by changing the order of import in App.js file, but later on its showing this error no matter what i do? please help in case I have any typing issue, if no then what is the problem

question from:https://stackoverflow.com/questions/65848744/searchchange-is-not-defined-no-undef

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your SearchBox should be:


const SearchBox = ({ searchfield, searchChange}) => {
return (
    <div className='pa2'>
        <input 
            className='pa3 b--green bg-light-blue'
            type='search'
            placeholder='search robots'
            onChange={searchChange}
        />
    </div>
);
}

export default SearchBox;

In your props there was searchChanger which is incorrect.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...