简体   繁体   English

React TypeError:无法读取未定义的属性“searchField”

[英]React TypeError: Cannot read property 'searchField' of undefined

TypeError: Cannot read property 'searchField' of undefined
Function.mapStateToProps [as mapToProps]
C:/Users/windw/Desktop/robofriends/src/containers/App.js:11
   8 | 
   9 | const mapStateToProps = state => {
  10 |     return {
> 11 |         searchField: state.searchRobots.searchField
  12 |     }
  13 | }
  14 | 

代码部分

Its evident that searchRobots does not exist on state/store or is empty.很明显, searchRobots不存在于状态/商店或为空。 Can you check to ensure you are referring to correct property searchRobots in state?您可以检查以确保您指的是正确的属性 searchRobots 状态吗?

use this searchField: state.searchField instead of searchField: state.searchRobots.searchField使用这个 searchField: state.searchField 而不是 searchField: state.searchRobots.searchField

And you instructor will also guide you to remove searchRobots in later part of video.您的讲师还将在视频的后面部分指导您删除 searchRobots。

I made a copy of your code and have attached working snippet here.我复制了您的代码并在此处附加了工作片段。 Only change you have to make is in the <SearchBox /> as shown.如图所示,您只需要在<SearchBox />进行更改。 I don't see why you are using redux though.我不明白你为什么要使用 redux。 Make sure to update the global state only if needed.确保仅在需要时更新全局状态。

Hope this is helpful!希望这是有帮助的!

 const CardList = ({ robots }) => { return ( <div> {robots.map((user, i) => { return ( <div key={i} className="bg-light-green dib br3 pa3 ma2 grow bw2 shadow-5" > <img src={`https://robohash.org/${user.id}?200x200`} alt="Robot Based On Char." /> <div> <h2>{user.name}</h2> <p>{user.email}</p> </div> </div> ); })} </div> ); }; const SearchBox = ({ searchfield, searchChange }) => { return ( <div> <input className="pa3 ba b--green bg-lighttest-blue" type="search" placeholder="search robots" onChange={e => searchChange(e)} /> </div> ); }; class App extends React.Component { constructor() { super(); this.state = { robots: [], searchfield: "" }; } componentDidMount() { fetch("https://jsonplaceholder.typicode.com/users") .then(response => response.json()) .then(users => this.setState({ robots: users })); } onSearchChange = event => { this.setState({ searchfield: event.target.value }); }; render() { const { robots, searchfield } = this.state; const filteredRobots = robots.filter(robot => { return robot.name.toLowerCase().includes(searchfield.toLowerCase()); }); return ( <div> <SearchBox searchChange={this.onSearchChange} /> <CardList robots={filteredRobots} /> </div> ); } } const rootElement = document.getElementById("root"); ReactDOM.render(<App />, rootElement);
 <div id="root"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.5.3/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/15.5.3/react-dom.min.js"></script>

For those following aneagoie course and still having this issue, the easiest hack you can do is to import searchRobots from reducers into your App.js and change对于那些遵循 aneagoie 课程但仍然有这个问题的人,你可以做的最简单的 hack 是将 searchRobots 从减速器导入你的 App.js 并更改

searchField: state.searchRobots.searchField

to

searchField: searchRobots(state.searchField)

This worked perfectly and easily for me.这对我来说完美而轻松。

you probably are doing a course from udemy.您可能正在学习 udemy 的课程。 That has this example.有这个例子。 Because I did it also.因为我也做过。 If you not, no worries, It´s the same exercise and I got the debugg for you.如果你没有,不用担心,这是同样的练习,我为你得到了调试。 When you use redux with just one global state, using createStore, you don´t add the variable searchRobots to the key searchField, instead, It should be state.searchField,当您仅使用具有一个全局状态的 redux 时,使用 createStore,您不会将变量 searchRobots 添加到键 searchField 中,而是应该是 state.searchField,

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM