我有一个导航栏,里面有一个表格。我希望将onSubmit上的所有数据发送到Firebase实时数据库。 但是,当我绑定onSubmit函数时,它什么也没有起作用。我的按钮或表单(不确定)在onSubmit上未检测到任何操作
我尝试将onSubmit字段直接放在Button中,结果相同,没有任何作用。 我尝试在handleSubmit函数中添加console.log,以查看当我单击Button时它是否正常工作,什么也没发生。我的控制台空着。我也将表格从导航栏中放出来只是为了进行测试。结果相同,无法正常工作。
有人有什么想法要发生吗? app.js
class App extends Component {
constructor(props) {
super(props);
this.state = {
sheetLoaded: false,
isNotClicked: true,
isMoreFiltersNotRequired: true,
handleShopClick: false,
selectedOption: "option1",
cabinePhoto: false,
bornePhoto: false,
helioBooth: false,
filteredResults: [],
filteredResultsLength: 0,
rating: 5,
shops: [],
loading: true,
zip_code: "",
searchResult:""
};
//To resolve clone App error when we are initializing App
if (!firebase.apps.length) {
firebase.initializeApp(config);
}
}
checkLoading = () => {
if (this.state.loading === true) {
console.log("loading...");
} else if (this.state.loading === false) {
console.log("Finish load");
}
};
getUserData = () => {
let ref = firebase.database().ref("shops");
ref.on("value", snapshot => {
const state = snapshot.val();
this.setState({
shops: state,
loading: false
});
});
};
componentWillMount() {
this.checkLoading();
this.getUserData();
}
handleChanges = e => {
const input = e.currentTarget;
const name = input.name;
const value = input.type === "checkbox" ? input.checked : input.value;
this.setState({ [name]: value });
};
handleSubmit = (e) =>{
console.log('search clicked')
e.preventDefault();
const itemsRef = firebase.database().ref('search');
const result = {
zip_code: this.state.zip_code,
selectedOption: this.state.selectedOption
}
itemsRef.push(result);
this.setState({
zip_code: '',
selectedOption: ''
});
}
app.js
[..........]
render() {
return (
<Router>
<HeaderFilters
wrapperHeaderFunction={this.wrapperHeaderFunction}
zip_code={this.state.zip_code}
handleChanges={this.handleChanges}
handleSubmit={this.handleSubmit}
isClicked={this.isClicked}
filterClick={this.filterClick}
selectedOption={this.state.selectedOption}
moreFilterClick={this.moreFilterClick}
filteredResults={this.state.filteredResults}
rating={this.state.rating}
startDate={this.state.startDate} // momentPropTypes.momentObj or null,
startDateId="your_unique_start_date_id" // PropTypes.string.isRequired,
endDate={this.state.endDate} // momentPropTypes.momentObj or null,
endDateId="your_unique_end_date_id" // PropTypes.string.isRequired,
onDatesChange={({ startDate, endDate }) =>
this.setState({ startDate, endDate })
} // PropTypes.func.isRequired,
focusedInput={this.state.focusedInput} // PropTypes.oneOf([START_DATE, END_DATE]) or null,
onFocusChange={focusedInput => this.setState({ focusedInput })} // PropTypes.func.isRequired,
/>
[.......]
导航栏组件
class HeaderFilters extends Component {
state = {};
render() {
return (
<React.Fragment>
<Navbar id="navbar" className="bg-light">
<Link to='/'><Navbar.Brand href="#home">Comparator-Booth</Navbar.Brand></Link>
<Form inline onSubmit={this.props.handleSubmit}>
{["radio"].map(type => (
<React.Fragment key={`custom-inline-${type}`}>
<Form.Check
custom
inline
label="Particuliers"
type={type}
id={`custom-inline-${type}-1`}
jsname="wCJL8"
name="selectedOption"
value="particuliers"
checked={this.props.selectedOption === "particuliers"}
onChange={this.props.handleChanges}
/>
<Form.Check
custom
inline
label="Pros"
type={type}
id={`custom-inline-${type}-2`}
name="selectedOption"
value="pros"
checked={this.props.selectedOption === "pros"}
onChange={this.props.handleChanges}
/>
</React.Fragment>
))}
</Form>
<Form inline>
<FormControl
placeholder="Code postal"
aria-label="Code postal"
aria-describedby="basic-addon1"
type="text"
name="zip_code"
value={this.props.zip_code}
onChange={this.props.handleChanges}
/>
<DateRangePicker
startDate={this.state.startDate} // momentPropTypes.momentObj or null,
startDateId="your_unique_start_date_id" // PropTypes.string.isRequired,
endDate={this.state.endDate} // momentPropTypes.momentObj or null,
endDateId="your_unique_end_date_id" // PropTypes.string.isRequired,
onDatesChange={({ startDate, endDate }) =>
this.setState({ startDate, endDate })
} // PropTypes.func.isRequired,
focusedInput={this.state.focusedInput} // PropTypes.oneOf([START_DATE, END_DATE]) or null,
onFocusChange={focusedInput => this.setState({ focusedInput })} // PropTypes.func.isRequired,
displayFormat={() => moment.localeData("fr").longDateFormat("L")}
/>
<Button type="submit"variant="success" onClick={this.props.filterClick} ><Link to = "/search" >Recherche !</Link></Button>
<Button variant="primary" onClick={this.props.moreFilterClick}>
Plus de filtres !
</Button>
</Form>
</Navbar>
</React.Fragment>
);
}
}
export default HeaderFilters;
答案 0 :(得分:0)
尝试删除<Link to = "/search" >
,我认为这可以防止在父级中发生点击
答案 1 :(得分:0)
尝试绑定您的提交功能
constructor(props) {
super(props);
this.state = {
sheetLoaded: false,
isNotClicked: true,
isMoreFiltersNotRequired: true,
handleShopClick: false,
selectedOption: "option1",
cabinePhoto: false,
bornePhoto: false,
helioBooth: false,
filteredResults: [],
filteredResultsLength: 0,
rating: 5,
shops: [],
loading: true,
zip_code: "",
searchResult:""
};
//To resolve clone App error when we are initializing App
if (!firebase.apps.length) {
firebase.initializeApp(config);
}
this.handleSubmit = this.handleSubmit.bind(this)
}