【发布时间】:2016-10-25 08:48:06
【问题描述】:
我有一个名为 List 的简单组件,它是一个简单的 ul,里面有一些 li。每个 li 都是一个简单的组件。 我有其他父组件呈现一个输入字段和 List 组件。点击 Send 键我捕捉到输入字段的文本。例如,我想调用一个名为 handleNewText(inputText) 的函数,但该函数需要保留在 List 组件中,因为我用来填充其他 li 组件的状态位于 >列表组件。
我不想重构 List 和 MyParent 组件,将数据管理从 List 传递到 MyParent。
第一个是父母,第二个是孩子
class TodoComp extends React.Component {
constructor(props){
super(props);
this.handleKeyPress = this.handleKeyPress.bind(this);
}
componentDidMpunt(){
console.log(this._child.someMethod());
}
handleKeyPress(event){
if(event.key === 'Enter'){
var t = event.target.value;
}
}
render(){
return (
<div>
<input
className="inputTodo"
type="text"
placeholder="want to be an hero...!"
onKeyPress={this.handleKeyPress}
/>
<List/>
</div>
);
}
}
export default class List extends React.Component {
constructor() {
super();
this.flipDone = this.flipDone.bind(this);
this.state = {
todos: Array(3).fill({ content: '', done: false})
};
}
flipDone(id) {
let index = Number(id);
this.setState({
todos: [
...this.state.todos.slice(0, index),
Object.assign({}, this.state.todos[index], {done: !this.state.todos[index].done}),
...this.state.todos.slice(index + 1)
]
});
}
render() {
const myList = this.state.todos.map((todo, index) => {
return (
<Todo key={index}
clickHandler={this.flipDone}
id={index}
todo={todo}
handleText={this.handleText}
/>
);
})
return (
<ul className="list">
{myList}
</ul>
);
}
ReactDOM.render(<TodoComp />,document.getElementById('myList'));
【问题讨论】:
-
你能展示你的代码吗?
标签: reactjs