我想传递的address
更新状态父组件,该子组件的center
财产。更具体地说,从App组件到BaseMap组件。但是,我似乎无法使其工作。新鲜的眼睛,可以帮助将不胜感激!通在更新父状态子成分反应JS
这是我的代码(我删除了取功能,它工作得很好,所以它不会打扰你,我只是需要更新我的孩子组件的状态。):
家长:
class App extends Component {
constructor(props) {
super(props);
this.state = {
avatar: '',
username: 'nickname93',
realName: '',
location: '',
followers: '',
following: '',
repos: '',
address: ''
}
}
render() {
return (
<div>
<SearchBox fetchUser={this.fetchUser.bind(this)}/>
<Card data={this.state} />
<BaseMap center={this.state.address}/>
</div>
);
}
fetchApi(url) {
// some fetch functionality
}
fetchUser(username) {
let url = `https://api.github.com/users/${username}`;
this.fetchApi(url);
}
componentDidMount() {
let url = `https://api.github.com/users/${this.state.username}`;
this.fetchApi(url);
}
}
和子组件:
export default class BaseMap extends React.Component {
constructor(props) {
super(props);
this.state = {
center: [24, 56],
zoom: 11
}
}
render() {
return (
<Col md={10} mdPull={1}>
<div className="map">
<GoogleMap
bootstrapURLKeys={'AIzaSyBhzwgQ3EfoIRYT70fbjrWASQVwO63MKu4'}
center={this.state.center}
zoom={this.props.zoom}>
</GoogleMap>
</div>
</Col>
);
}
}