【发布时间】:2019-09-21 15:33:08
【问题描述】:
我已经编写了基于功能的组件,我正在检查开关盒中的类型,这取决于我在此处创建表单的类型
const component = (props) => {
let renderObject = null
let obj;
const renderdata = () =>{
console.log(props);
obj = Object.keys(props.todoprop).map((todoproprties) =>{
console.log("here we are looking at the data" , props.todoprop[todoproprties])
let toDoProprties = props.todoprop[todoproprties]
switch(props.todoprop[todoproprties].type){
case 'text':
return <input type="text" key="1" value={toDoProprties.value} name = {toDoProprties.name} onChange={props.handleChange} />
case 'date':
return <input type="date" key="2" value={toDoProprties.value} onChange={props.handleChange}/>
default:
return <textarea rows="10" key="3" cols="30" rows="10" cols="30" value={toDoProprties.value} onChange={props.handleChange}/>
}
})
}
return (
<div>
<h1>Hello from component </h1>
{renderdata()}
<div className="card">
<div className="card-body">
<div>
{obj}
<button type="submit" onClick={props.handleSubmit}>Submit</button>
</div>
</div>
</div>
{console.log(props)}
</div>
);
}
export default component;
下面是渲染我的组件的容器
class container extends Component {
state = {
// how i want my data to look with a heading with date proprtey and a description
heading: '',
date: '',
description: '',
Todo: {
heading: { type: "text", value: "", placeholder: 'plese enter heading', name:"heading"},
date: { type: "date", value: "", placeholder: "plese enter date", name: "date" },
description: { value: "PLESE DESIGN A GOOD TODO", placeholder: 'plese enter description', name: "description"}
}
}
onChangeHandler = (event) =>{
console.log(event.value)
this.setState({
[event.target.name] : event.target.value
})
// this.setState({Todo.heading.value: event.target.value })
}
onSubmitHandler = () =>{
console.log("you have succesfully clicked the handler")
console.log("you are seeing the state" ,this.state.heading);
console.log("you are seeing the description", this.state.description)
}
render() {
var dataPassingHandler = () => {
return (<div>
<Dynamiccomponent todoprop={this.state.Todo} handleChange={this.onChangeHandler} handleSubmit={this.onSubmitHandler}/>
</div>
)
}
return (
<div>
{dataPassingHandler()}
</div>
)
}
}
export default container;
1) 在我的容器中,我正在渲染组件,我正在将类型传递给容器,我正在检查 switch case 我正在创建表单
2)我被困在这里有两个问题第一个是如何通过 onChange ,
3)其次如何处理onclick并在容器状态下创建一个新对象
【问题讨论】:
-
有什么问题?你的表格正确吗?
-
我无法在我的输入字段中输入任何内容
-
为什么会有复杂的 Todo 状态?不能直接有Todo数组吗?
-
是的,但我想学习(我是新来的反应)第一次尝试我使用数组接下来我想尝试如果给定对象类型如何提取数据然后我继续流程(我忘了改变我教它的工作并继续)
-
一切都好,我只想知道如何动态添加待办事项的状态并使 onChange 工作