【发布时间】:2018-11-03 07:02:20
【问题描述】:
我有像下面这样的组件,它有 4 个状态值
class ComposedTextField extends React.Component {
state = {
name: '',
title: '',
email: '',
experience: ''
};
handleChange = event => {
this.setState({
[event.target.name]:event.target.value,
[event.target.title]:event.target.value,
[event.target.email]:event.target.value,
[event.target.experience]:event.target.value
});
};
render() {
const { classes } = this.props;
return (
<div>
<Typography variant="headline">Header Info</Typography>
<FormControl fullWidth className={classes.formControl}>
<InputLabel htmlFor="name-simple">Name</InputLabel>
<Input name="name" value={this.state.name} id="name-simple" onChange={this.handleChange}/>
</FormControl><br></br>
<FormControl fullWidth className={classes.formControl}>
<InputLabel htmlFor="title-simple">Title</InputLabel>
<Input name="title" id="title-simple" value={this.state.title} onChange={this.handleChange}/>
</FormControl><br></br>
<FormControl fullWidth className={classes.formControl}>
<InputLabel htmlFor="email-simple">Email</InputLabel>
<Input name="email" id="email-simple" value={this.state.email} onChange={this.handleChange}/>
</FormControl><br></br>
<FormControl fullWidth className={classes.formControl}>
<InputLabel htmlFor="experience-simple">Experience</InputLabel>
<Input name="experience" id="experience-simple" value={this.state.experience} onChange={this.handleChange}/>
</FormControl><br></br>
</div>
);
}
}
我需要将这 4 个值传递给另一个组件
function Header(props) {
const { classes, avatar } = props;
return (
<div>
<Typography variant="headline">KASUN FERNANDO</Typography>
<Typography variant="subheading" color="textSecondary">
SENIOR DESIGNER-UI/UX
</Typography>
<Typography variant="subheading" color="textSecondary">
mickey@crowderia.com
</Typography>
<Typography variant="subheading" color="textSecondary">
4+ years of experience
</Typography>
</div>
);
}
在这个组件中有 4 个排版,我需要在 4 个排版中显示该状态值
我该怎么做,请帮助我是 React js 的新手
【问题讨论】:
-
如果Header组件和composedTextField无关,要么需要将数据存储在一个普通的parent中,要么使用redux、mobx。检查stackoverflow.com/questions/46594900/…
标签: javascript reactjs state