我有一個AdminLayout的反應是又加載子元素以這種方式:傳遞道具克隆子元素給未知道具警告
class AdminLayout extends Component {
constructor(props){
super(props)
this.state = {
flash: { msg: "some message", type: undefined}
}
}
updateFlash(flash){
this.state.flash = flash
}
render() {
return(
<div>
<FlashMessage flash={this.state.flash} />
<NavBar/>
{ React.cloneElement(this.props.children, {updateFlash: this.updateFlash})}
</div>
)
}
}
這裏的想法是,我打算讓我的子元素更新AdminLayout中的Flash狀態。然而,隨着代碼{updateFlash: this.updateFlash}
我收到以下錯誤:
Warning: Unknown prop `updateFlash` on <div> tag. Remove this prop from the element. For details, see https://facebook.github.io/react/warnings/unknown-prop.html
in div (created by Grid)
in Grid (created by LocationEdit)
我看過的文件,我不認爲我做的任何案件。我也沒有試圖直接在任何div上設置道具。
有沒有人見過這樣的事情?
Hrm ...它也可能有助於知道我的內部組件是什麼樣子。我試着濾出來任何非必要...:
import React, {Component} from 'react'
import AdminLayout from '../components/layouts/AdminLayout'
import LocationsSource from '../sources/LocationsSource'
import {Button, Grid, Row, Col, FormGroup, FormControl, ControlLabel, HelpBlock} from 'react-bootstrap'
import { browserHistory } from 'react-router'
class LocationEdit extends Component {
constructor(props) {
super(props)
this.handleSubmit = this.handleSubmit.bind(this)
this.state = {
loading: true,
location: {
name: "",
site_link: "",
description: ""
}
}
}
handleSubmit = (event) => {
event.preventDefault()
let data = this.state.location
if(data._id != undefined) {
LocationsSource.update(data).then((response) => {
browserHistory.push('/admin/locations/');
})
} else {
LocationsSource.create(data).then((response) => {
browserHistory.push('/admin/locations/');
})
}
}
componentDidMount() {
if(this.props.params.id)
LocationsSource.find(this.props.params.id).then((result) => {
this.setState({loading: false, location: result})
})
else{
this.setState({loading: false, location: {}})
}
}
handleChange = (event) => {
let change = {location: this.state.location}
change['location'][event.target.name] = event.target.value
this.setState(change)
}
componentWillUnmount() {}
render() {
let location = this.state.location
if(this.state.loading)
return(
<AdminLayout>
<Row>
<img src="/assets/images/loading.gif" />
</Row>
</AdminLayout>
)
else
return(
<AdminLayout>
<Grid>
<Row>
<Col xs={8} md={8}>
<h2>Location</h2>
</Col>
</Row>
<Row>
<Col xs={8} md={8}>
<form onSubmit={this.handleSubmit}>
<FormGroup controlId={"name"}>
<ControlLabel>Name:</ControlLabel>
<FormControl onChange={this.handleChange} name="name" type="text" defaultValue={location.name} />
</FormGroup>
<Button type="submit">
Submit
</Button>
</form>
</Col>
</Row>
</Grid>
</AdminLayout>
)
}
}
export default LocationEdit
什麼是'updateFlash'和什麼是'flash'參數?你不能像你那樣設置狀態。 – Chris
@Chris updateFlash是一個函數,當我想更新佈局中的Flash消息時,我想從子組件調用它。理想情況下,我想將Flash消息信息傳遞給管理員,以便實習生將其傳遞給Flash消息組件。 – unflores