javascript - 如何将值从反应组件返回到调用模块?
问题描述
我有以下代码用于选择一周中的某一天或几天,某些特价商品将可用。我将在整个应用程序中重用此代码。我遇到的问题是我无法确定如何将真/假值返回给调用模块。
DOW 组件的精简版:
import React from "react";
class DOW extends React.Component {
constructor(props) {
super(props);
this.state = {
daily: true,
sun: false,
mon: false,
tue: false,
wed: false,
thu: false,
fri: false,
sat: false
};
this.handleInputChange = this.handleInputChange.bind(this);
}
handleInputChange(event) {
const target = event.target;
const value = target.type === 'checkbox' ? target.checked : target.value;
const name = target.name;
this.setState({
[name]: value
});
}
render() {
return (
<div>
<label>
Daily:
<input
name="daily"
type="checkbox"
checked={this.state.daily}
onChange={this.handleInputChange} />
</label>
<span> </span>
<label>
Sun:
<input
name="sun"
type="checkbox"
checked={this.state.sun}
onChange={this.handleInputChange} />
</label>
<span> </span>
<label>
Mon:
<input
name="mon"
type="checkbox"
checked={this.state.mon}
onChange={this.handleInputChange} />
</label>
<span> </span>
...
<label>
Sat:
<input
name="sat"
type="checkbox"
checked={this.state.sat}
onChange={this.handleInputChange} />
</label>
<span> </span>
<br />
</div>
);
}
}
export default DOW;
=======
调用组件的片段:
import React, { Component } from "react";
import Nav from "../../components/Nav";
import Location from "../../components/Location";
import DeleteBtn from "../../components/DeleteBtn";
import Jumbotron from "../../components/Jumbotron";
import DOW from "../../components/DOW";
import API from "../../utils/API";
import { Link } from "react-router-dom";
import { Col, Row, Container } from "../../components/Grid";
import { List, ListItem } from "../../components/List";
import { Input, TextArea, FormBtn } from "../../components/Form";
class FoodSpecials extends Component {
state = {
foodSpecial: [],
special: "",
price: "",
details: ""
};
componentDidMount() {
this.loadfoodSpecial();
}
handleInputChange = event => {
const { name, value } = event.target;
this.setState({
[name]: value
});
};
handleFormSubmit = event => {
event.preventDefault();
if (this.state.special && this.state.price) {
console.log(this.state.special, this.state.price, this.state.daily, this.state.mon);
}
};
render() {
return (
<Container fluid>
...
<Col size="md-12">
<h4>
Day of week:
</h4>
<DOW />
</Col>
...
</Col>
</Row>
</Container>
);
}
}
export default FoodSpecials;
我环顾四周寻找一个 NPM,它只显示带有复选框的星期几,但一直找不到一个,这就是我尝试这段代码的原因。
提前致谢。
解决方案
在您的 FoodSpecials 类中,添加一个函数:
getDOWs = days => {
console.log(days.wed); //etc...
}
然后将此函数传递给子组件:
<DOW onInputChange={this.getDOWs} />
然后最后从 DOW 类中调用你的函数:
handleInputChange(event) {
const target = event.target;
const value = target.type === 'checkbox' ? target.checked : target.value;
const name = target.name;
this.setState({
[name]: value
});
this.props.onInputChange(this.state); //This is where you call the function.
}
推荐阅读
- android - 使用共享首选项在 Firebase 身份验证中进行登录注册
- deep-learning - Unet 细分模型的负面示例的相关性如何?
- javascript - 如何从 JSF Bootsfaces 检索输入文本值到 javascript 变量
- javascript - 如何在字符串中的数字中添加逗号
- python - Python代码的组织。一个大文件与多个模块、包
- vue.js - Vue.js 延迟加载路由错误处理
- android - 如果lifecycleScope是主管,为什么它的子协程失败导致应用程序崩溃?
- laravel - 如何在子域中托管 laravel 应用程序
- python - 从 Python 中的 Amazon Kinesis 输出读取字节图像
- javascript - 使用“throw”返回函数结果是使用 throw 的正确方法吗?