用编辑和存储状态反应基本的待办事项列表

时间:2018-11-22 11:34:15

标签: reactjs

I need some help building a todo list with React. Im a beginner so there are a lot of aspects of react I don't understand. I've created a Todo list at the moment this lists an un ordered list with the input, edit and remove button a select menu and a further input field.

首先,我对我更新状态的位置感到困惑。 “ App.js”是我的主要状态存储在的位置,我试图保持这种状态。我需要知道如何编辑存储新值的输入字段(待办事项列表项)。林然后想创建一个“完成的列表”,我要在其中存储输入字段以及选择选项(曾经被单击),请有人可以给我一些指导。预先谢谢

import React, { Component } from 'react';
import Form from './Components/Form'
import './App.css';
import List from './Components/List'
import Completed from './Components/Completed'


class App extends Component {

constructor(props){
    super(props)
 this.state={
        isEditing:false,  
        text:"",

        items:[],
        completed:[
        {

      }
        ]

    }

this.submit=this.submit.bind(this);
this.eventHandler=this.eventHandler.bind(this)

}

submit=(e)=>{
e.preventDefault();
this.setState({

  items:[
        {
         name:this.state.text,


        },
        ...this.state.items
      ],
  text:""
})


}



remove=(index)=>{
  this.setState({
    items:this.state.items.filter((_,i) => i!==index)
   })
}



onChange=(index)=>{


this.setState({
    items:this.state.items.filter((_,i) => i!==index)


  });


}



eventHandler=(e)=>{

this.setState ({
text:e.target.value

})

}

handleNameEdits=()=>{

this.setState({

isEditing:true


 })

}

edit=()=>{
  this.setState({

isEditing:!this.state.isEditing
})
}

myoptions=(e)=>{
this.setState({
completed:[
{

  webmaster:e

},
...this.state.completed

]
})


}





render() {


   return (

     <div className="App">
       <header className="App-header">
         <Form submit={this.submit} myValue={this.state.text} eventHandler= 
           {this.eventHandler}/>
        {this.state.items.map && this.state.items.map((item,index)=>(
            <List key={index} 
              name={item.name} 
              edit={this.edit}  
              change={()=>this.onChange(index)} 
              remove={()=>this.remove(index) }
              handleNameEdits={this.handleNameEdits}
              myoptions={(e =>this.myoptions(e.target.value))}

              />
        ))}
        </header>

      <div>
        completed

      </div>


       </div>
      );
    }
  }

export default App;

 import React, { Component } from 'react';
 import PropTypes from 'prop-types';
 import Edit from './Edit';

 class List extends Component { 

constructor(props) {
super()



    this.options = [
        {name:'web1'},
        {name:'web2'},
        {name:'web3'},
        {name:'web4'}
     ];
}


render() {

    const {key} = this.props;
    const x=this.options;






    return (
    <ul>
                <li>{this.props.name} 
                        <button onClick={this.props.edit}>Edit</button>
                        <button onClick={this.props.remove}>Remove</button>
                        <select onChange={this.props.myoptions}> 
 {this.options.map(options => <option>{options.name}</option> )} 

                                </select>
                        <label> Completed 
                        </label><input type="checkbox" onChange= 
 {this.props.change} checked={this.props.change} onClick= 
   {this.props.submit}/>
                        <label> Ticket Number </label><input type='text'/>







                </li>




    </ul>
    )



   }
 }




export default List;
import React from 'react'
import PropTypes from 'prop-types';


 const Form= props=> {

 return (

   <form onSubmit={props.submit}>
      <input type='text' value={props.myValue} onChange= 
        {props.eventHandler}/>
     <button> click</button>


 </form>




   )





  }


Form.PropTypes={
onSubmit:PropTypes.func.isRequired,
evenHandler:PropTypes.func.isRequired,
myValue:PropTypes.string.isRequired

 }




export default Form

0 个答案:

没有答案