javascript - 如何将 React 状态传递给另一个组件中的嵌套组件
问题描述
我有一个用户组件,它制作一个 API 来获取信息,然后将其存储在状态中。问题是我无法弄清楚如何将状态传递给该组件内的另一个组件<FormInputs>
所以,在调用之后,我需要让参数state: users[]
能够显示在子组件的“defaultValue”属性中。我该怎么做呢?我尝试了许多不同的方法,但总是“未定义”
我也试过这个:defaultValue: `${users.email}
没有骰子。也试过this.props.users.email
没有骰子。提前感谢您提供可能的提示!
import axios from 'axios';
import {
Grid,
Row,
Col,
FormGroup,
ControlLabel,
FormControl
} from "react-bootstrap";
import { Card } from "components/Card/Card.jsx";
import { FormInputs } from "components/FormInputs/FormInputs.jsx";
import { UserCard } from "components/UserCard/UserCard.jsx";
import Button from "components/CustomButton/CustomButton.jsx";
import avatar from "assets/img/faces/face-3.jpg";
class UserProfile extends Component {
constructor(props) {
super(props);
this.state = {
users: []
};
}
componentDidMount() {
axios.get('https://niftyAPIurlDataCall/AlltheThings')
.then(response => {
console.log(response.data);
const users = response.data;
this.setState({ users });
})
.catch(error => {
console.log(error);
});
}
render() {
return (
<div className="content">
<Grid fluid>
<Row>
<Col md={8}>
<Card
title="Edit Profile"
content={
<form>
<FormInputs
ncols={["col-md-5", "col-md-3", "col-md-4"]}
properties={[
{
label: "Lab (disabled)",
type: "text",
bsClass: "form-control",
placeholder: "Lab",
defaultValue: "account",
disabled: true
},
{
label: "ID",
type: "text",
bsClass: "form-control",
placeholder: "ID",
defaultValue: users.id
},
{
label: "Email address",
type: "email",
bsClass: "form-control",
placeholder: "Email",
defaultValue: `${users.email}`
}
]}
/>
<FormInputs
ncols={["col-md-6", "col-md-6"]}
properties={[
{
label: "First name",
type: "text",
bsClass: "form-control",
placeholder: "First name",
defaultValue: `${users.first_name}`
},
{
label: "Last name",
type: "text",
bsClass: "form-control",
placeholder: "Last name",
defaultValue: `${users.last_name}`
},
{
label: "User ID",
type: "text",
bsClass: "form-control",
placeholder: "Last name",
defaultValue: `${users.user_id}`
}
]}
/>
<FormInputs
ncols={["col-md-12"]}
properties={[
{
label: "Group",
type: "text",
bsClass: "form-control",
placeholder: "Group",
defaultValue: `${users.group}`
},
{
label: "Group ID",
type: "text",
bsClass: "form-control",
placeholder: "Group ID",
defaultValue: `${users.group_id}`
},
,
{
label: "Join date",
type: "text",
bsClass: "form-control",
placeholder: "Join date",
defaultValue: `${users.join_date}`
}
]}
/>
<Button bsStyle="info" pullRight fill type="submit">
Update Profile
</Button>
<div className="clearfix" />
</form>
}
/>
</Col>
</Row>
</Grid>
</div>
);
}
}
export default UserProfile;
解决方案
推荐阅读
- java - SHA256 散列到 .Net 散列
- sql-server - 当时间格式不同时,Datetime 为 NULL
- r - 在 R 中对多个字符串条件使用查找和替换
- cassandra - 从 Apache cassandra 3.11.4 迁移到 4.0 beta 版本时出现问题
- python - 用股票创建数据框
- java - 需要帮助尝试格式化日期时间
- jquery - 联系表格 7 发送一次表格,虽然客户点击了几次
- reactjs - 一旦触发显示,如何扩展 React DatePicker 弹出窗口?
- c# - 干净的代码:如何重构代码以使其更好地删除 switch 语句
- r - 在 R 中读取 .xls 文件