reactjs - 在反应中将数据从一个组件发送到另一个组件
问题描述
我已经看到了多个相同的问题,然后是这个问题,但我不明白他们的答案。
我有一个组件可以获取并获取一些数据。我想将此数据与其他信息一起发送给另一个组件(孩子?我不太明白这一点)。
这是我的第一个组件: (login.component.jsx) 我想将 IP、用户名、密码、CPGlist 传递给 CreateVolume 中的组件并在那里使用它。
class Login extends React.Component {
constructor(props) {
super(props);
this.state = {
IP: "xxx",
username: "",
password: "",
submitted: false,
CPGlist: ""
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
.
.
.
render() {
const { IP, username, password, CPGlist, submitted } = this.state;
if (submitted) {
return (
<div>
< CreateVolume />
</div>
);
}
CreateVolume.component.jsx:
class CreateVolume extends React.Component {
constructor(props) {
super(props);
this.state = {
IP: "",
username: "",
password: "",
submitted: false,
name: "",
cpg: "",
size: "",
unit: ""
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
.
.
.
//some code
我应该能够从 login.component.jsx 获取 IP/用户名/密码/CPGlist 到 CreateVolume.component.jsx
谢谢
解决方案
<CreateVolume />
仅当填充状态中的数据时才挂载。
{
this.state.ip && this.state.ip!="" &&
<CreateColume
IP = {this.state.IP}
...
/>
}
推荐阅读
- dart - 为什么 Dart 中的单元化类型变量不实现类型的接口?
- ocaml - 在 OCaml Menhir 中,如何为 C++/Rust/Java 风格的泛型编写解析器
- python - 使用 make_lupton_rgb 获取深色图像
- android - ConstraintLayout 中的 MoPubRecyclerAdapter 和 Facebook 原生广告崩溃
- python - Keras 错误:dense_3 层的输入 0 与层不兼容::预期 min_ndim=2,发现 ndim=1。收到的完整形状:[16]
- algorithm - 如何设计使用 A* 寻路算法找到 FASTEST 路线的成本函数和启发式函数?
- ios - 嵌套的 UICollectionView 为每个部分返回相同的值 - Swift
- python-3.x - 为每个列表列表造一个词
- algorithm - 最长回文子串。以下 DP + Recursive 方法的运行时间可能是多少。是 O(n^2) 还是 O(n^3)
- swift - 如何解决 WKWebView 较小的内容高度值问题?