我的组件的render()
方法在使用this.setState()
.
这是更改组件状态的方法(它被调用并且我已经验证了这一点):
handlePhoneNumberChange = name => (event) => {
const { contactPhone } = this.state;
const index = name;
const phoneEntry = contactPhone[index];
phoneEntry.phoneNumber = event.target.value;
this.setState({ contactPhone });
};
这是组件的初始状态:
constructor(props) {
super(props);
this.state = {
contactPhone: [{ phoneNumber: '', phoneType: '' }],
};
}
这是 TextField 的呈现方式:
{contactPhone.map((phone, index) => (
<div key={index}>
<TextField
id={String(Math.random())}
label="Phone number"
type="tel"
value={phone.phoneNumber}
onChange={this.handlePhoneNumberChange(index)}
placeholder="Contact phone number"
margin="normal"
/>
<br />
</div>
))}
为什么在 setState() 之后不调用渲染?