我從發送 JSON 數據的本地 API 服務器獲取了用戶數據,并且在我添加搜索功能之前它就可以工作了。我認識到這些錯誤發生的問題來自React的生命周期風格,但是因為我對React研究的不多,所以我提出問題來與您解決這個問題。macOS Mojave、Express.js、React.js 和 React-Router 正在運行我的本地開發服務器。我以前從未嘗試過此搜索功能。這些是我的代碼:用戶列表.js:import React, {Component} from 'react';import {List, Avatar, Button, Modal, Input} from 'antd';import UserItem from "../components/UserItem";import blue_check from '...';const {Search} = Input;class UsersList extends Component { state = { users: [], modalVisible: false, dataKey: 0, keyword: '', }; constructor(props) { super(props); this.state = { users: [{ member_idx: 0, nickname: 'loading...', }] }; } componentDidMount() { fetch('/api/getUsers') .then(res => res.json()) .then(users => this.setState({users: users})); } showModal = (key) => { this.setState({ modalVisible: true, dataKey: key }); }; handleClose = e => { console.log(e); this.setState({ modalVisible: false }); }; handleSearch = (e) => { this.setState({ keyword: e.target.value, }) }; render() { const {users, keyword} = this.state; const filteredUsers = users.filter( user => user.nickname.indexOf(keyword) !== -1 ); // This line makes error return ( <div> <Search placeholder="Input Search Text" enterButton="Search" onSearch={value => this.state.handleSearch(value)} size="large" />UserItem.js 在這篇文章中不需要,因為它沒有關于這個錯誤的問題。
無法讀取 null 的屬性“indexOf”,預計是反應生命周期問題
收到一只叮咚
2021-08-26 16:38:36