0
我一直在嘗試添加一個選項到食物列表來添加其他食物與輸入字段。每次我進入時,它不僅帶走了輸入字段,而且還帶走了食物清單。我不確定我在這裏做錯了什麼。反應輸入字段沒有添加到已經呈現的列表
這裏是我的搜索欄組件:
import React, { Component } from 'react';
class SearchBar extends Component {
constructor(props) {
super(props)
this.state = {
inputValue: ''
}
this.handleChange = this.handleChange.bind(this);
this.addItem = this.addItem(this);
}
handleChange(event) {
const inputTerm = event.target.value;
this.setState({
inputValue: inputTerm
})
console.log(this.state.inputValue)
}
addItem(props) {
const food = this.props.food;
food.push(this.state.inputValue)
}
render() {
return (<div>
<form
className="form"
onClick={this.addItem}>
<div className="edit-input-div">
<input type='text' value={this.state.inputValue}
onChange={this.handleChange}
className='form-control'
placeholder="AddItem"
/>
<span>
<button onClick={this.addItem} className="add-new-item">Add Food</button>
</span>
</div>
</form>
</div>
)
}
}
export default SearchBar;
它正在這裏呈現:
import React from 'react';
import shortid from 'shortid';
import SearchBar from './SearchBar';
const TotalListItem = (props) => {
return (
<ul className="cook-list" id={props.index}>
{props.food.map((item, index) => {
return (
<li className="cook-list-items" key={shortid.generate()} id={index}>{item.title}</li>
)
})}
<button className="delete" onClick={() => props.onDelete(props.food)}>Delete Order</button>
<button className="complete" onClick={() => props.onComplete(props.food)}>Complete Order</button>
<SearchBar
food={props.food}
/>
</ul>
);
};
export default TotalListItem;
其正在這裏呈現:
import React from 'react';
import shortid from 'shortid';
import TotalListItem from './TotalListItem';
const Total = (props) => {
if (props.cookList.length === 4) {
alert('the manager has been alerted')
}
const renderProps = props.cookList.map((food, index) => {
return (
<TotalListItem
index={index}
food={food}
key={shortid.generate()}
onDelete={props.onDelete}
onComplete={props.onComplete}
/>
);
})
return (
<div>
<div>{renderProps}</div>
</div>
)
}
export default Total;
我在'SearchBar'組件中注意到的第一件事就是你試圖給道具增加價值(錯誤的方法)。永遠記住「道具下來,事件起來」 – ghostffcode