Javascript 反应类型错误:无法读取属性';搜索字段';未定义的

Javascript 反应类型错误:无法读取属性';搜索字段';未定义的,javascript,reactjs,Javascript,Reactjs,很明显,searchRobots在state/store上不存在或为空。您能检查一下以确保您所指的是状态正确的属性searchRobots吗?我复制了您的代码,并在这里附上了工作片段。您只需在中进行更改,如图所示。我不明白你为什么要使用redux。确保仅在需要时更新全局状态 希望这是有帮助的 const CardList=({robots})=>{ 返回( {robots.map((用户,i)=>{ 返回( {user.name} {user.email} ); })} ); }; const

很明显,
searchRobots
在state/store上不存在或为空。您能检查一下以确保您所指的是状态正确的属性searchRobots吗?

我复制了您的代码,并在这里附上了工作片段。您只需在
中进行更改,如图所示。我不明白你为什么要使用redux。确保仅在需要时更新全局状态

希望这是有帮助的

const CardList=({robots})=>{
返回(
{robots.map((用户,i)=>{
返回(
{user.name}
{user.email}

); })} ); }; const SearchBox=({searchfield,searchChange})=>{ 返回( 搜索更改(e)} /> ); }; 类应用程序扩展了React.Component{ 构造函数(){ 超级(); 此.state={ 机器人:[], 搜索字段:“ }; } componentDidMount(){ 取回(“https://jsonplaceholder.typicode.com/users") .then(response=>response.json()) .then(users=>this.setState({robots:users})); } onSearchChange=事件=>{ this.setState({searchfield:event.target.value}); }; render(){ const{robots,searchfield}=this.state; 常量过滤器机器人=机器人。过滤器(机器人=>{ return robot.name.toLowerCase().includes(searchfield.toLowerCase()); }); 返回( ); } } const rootElement=document.getElementById(“根”); render(,rootElement)

使用此 searchField:state.searchField 而不是 searchField:state.searchRobots.searchField


您的讲师还将在视频的后面部分指导您删除searchRobots。

对于那些学习了aneagoie课程但仍然存在此问题的人,您可以做的最简单的黑客操作是将searchRobots从Reducer导入您的App.js并进行更改

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 | 


这对我来说既完美又容易

您可能正在上udemy的课程。这就是一个例子。因为我也这么做了。如果你没有,不用担心,这是同样的练习,我给你调试了。当您仅使用一个全局状态使用redux时,使用createStore,您不会将变量searchRobots添加到key searchField,相反,它应该是state.searchField,

对不起,搜索字段而不是下面的图片。你可以复制粘贴你的组件代码而不是图片吗?在
状态中没有
searchRobots
,这是项目的链接。我添加了完整的项目和文件,因为我是新的,仍然不理解alil你的意思。我在你的回复中没有看到任何重复代码。我的意思是,商店似乎没有searchRobots属性。我在文件中找不到任何错误说明的redux代码。是的,在他给我们的屏幕截图中,他添加了一些redux代码。我不明白他为什么这么做。事实上,我补充说,这是我学习如何使用它的课程的一部分,但老实说,我还是不明白他为什么把它放在那里。哈哈:)好的。在我给出的上述解决方案中,我们只是将函数绑定到onclick处理程序。
searchField: state.searchRobots.searchField
searchField: searchRobots(state.searchField)