87,841
社区成员




class ProductCell extends PureComponent{
constructor(props){
super(props);
this.state={
value:'',
isActive:false,
fromParent:false
}
this.changeHandle=this.changeHandle.bind(this);
}
static getDerivedStateFromProps(nextProps,preState){
if(preState.isActive){
return {isActive:!preState.isActive,fromParent:true};
}
return null;
}
changeHandle(e){
this.setState({value:e.target.value,isActive:true,fromParent:false});
}
render(){
const {id,func}=this.props;
if(this.state.fromParent){
func(id,this.state.value);
}
return[
<label>{id}</label>,
<input value={this.state.value} onChange={this.changeHandle}/>
]
}
}
class ProductTable extends Component{
constructor(props){
super(props);
this.state={
reRender:false
}
this.clickHandle=this.clickHandle.bind(this);
this.showChange=this.showChange.bind(this);
}
clickHandle(){
this.setState({reRender:true});
}
showChange(id,value){
console.log(id,value);
}
render(){
const arr=Array.from({length:this.props.length},(item,index)=>(<ProductCell func={this.showChange} key={index} id={index} />));
arr.push(<input type={'button'} value={'save'} onClick={this.clickHandle} />);
return arr;
}
}
ReactDOM.render(<ProductTable length={10}/>,document.body);
这样试试