그래서, 내 구성 요소는 상위 구성 요소에서 개체를 수신 객체가이 모양 렌더링 :ReactJS는 : 배열에 객체를 켜고
내 아이 구성 요소에서{
_id: Data.now(), // MongoDB database
name: "",
description: "",
image: "images/image.jpg",
type: "image"
}
, 나는이 데이터를 받아와를 만들고 싶어 각 하나에 대한 입력 값을 변경하고 새 데이터를 저장할 수 있습니다.
업데이트 응답 (ES6 클래스) :
constructor(props) {
super();
this.state = {
fieldGroups: []
}
this.parseProps = this.parseProps.bind(this);
}
componentWillMount() {
this.parseProps(this.props);
}
componentWillReceiveProps(nextProps) {
this.parseProps(nextProps);
}
parseProps(props) {
var fieldsArray = [];
var content = props.content;
Object.keys(content).map((field,index) => {
if (field === 'type') {
let fieldObj = {};
fieldObj.field = field;
fieldObj.value = content[field];
fieldObj.key = props.content._id + field;
fieldsArray.push(fieldObj);
}
});
this.setState({
fieldGroups: fieldsArray
});
}
render() {
return (
{
this.state.fieldGroups.map((field) => {
if (field.field === "type") {
return (html element specific to type)
} else {
return (a different html element)
}
})
}
)
}
그래서 지금은 아이 컴포넌트는 사용자를 보여 필드 결정 댄다는 의미에서 내 구성 요소를 분리 할 수 있습니다. 감사합니다 DanneManne
불완전한 예를 들어 설명하기가 어렵습니다. 이 구성 요소에 대해 더 많은 코드를 표시 할 수 있습니까? 수업입니까? 생성자를 올바르게 구현 했습니까? https://stackoverflow.com/help/mcve –