[英]how to send to other component onChange select in react
i just try to do something like that (when user select, continue to other component): 我只是尝试做这样的事情(当用户选择时,继续其他组件):
render() {
return(
<select onChange={(e)=> <CountrySelected countrySelected=
{e.target.value}/> }>
{this.state.countryArray.map( (e) => <option> {e.name} </option>) }
</select>
);
}
just if i can't do it, so pls continue reading the next code to tell me how can i solve the follow problem: The following code work well for me: 就算我做不到,也请继续阅读下一个代码,告诉我如何解决以下问题:以下代码对我来说很有效:
class App extends Component {
state = {
countryArray: [],
selectedCountry: undefined
};
constructor(p) {
super(p);
// here i'm Initializing the this.state.countryArray
}
countrySelectionHandler=(countryName)=> {
this.setState({ selectedCountry:
this.state.countryArray.find(e=>e.name===countryName) });
}
render() {
return (
<div>
<CountryDropDown
countryArray={this.state.countryArray}
countrySelectionHandler={this.countrySelectionHandler} />
{
this.state.selectedCountry ?
// this component just display on the screen the selectedCountry name
<CountryName countryName={this.state.selectedCountry.name} />
:
<div> no country selected</div>
}
</div>
);
}
}
'CountryDropDown' component: “ CountryDropDown”组件:
const countryDropDown = (p)=>(
<select onChange={(e)=> p.countrySelectionHandler(e.target.value)}>
{p.countryArray.map( (e) => <option> {e.name}
</option>) }
</select>
)
but now when user select, it's will rerender 'CountryDropDown' components for no reason. 但是现在,当用户选择时,它将无缘无故地重新渲染“ CountryDropDown”组件。 so how can i tel react to rerender only this part:
所以我该如何打电话以仅重新渲染此部分:
{
this.state.selectedCountry ?
// this component just display the selectedCountry name
<CountryName countryName={this.state.selectedCountry.name} />
:
<div> no country selected</div>
}
so the way i try to solve it: when user select, continue to other components(CountrySelected) and there render only 'CountryName' component 所以我尝试解决它的方式:当用户选择时,继续使用其他组件(CountrySelected),然后仅呈现“ CountryName”组件
If you do not use PureComponent or custom shouldComponentUpdate lifecycle function. 如果不使用PureComponent或自定义shouldComponentUpdate生命周期函数。 Children component will be re-rendered when parent component re-render.
当父组件重新渲染时,子组件将被重新渲染。
class CountryDropDown extends Component {
shouldComponentUpdate(nextProps) {
return (this.props.countryArray !== nextProps.countryArray)
}
render() {
const { countrySelectionHandler, countryArray } = this.props
return (
<select onChange={countrySelectionHandler}>
{countryArray.map((e) => <option> {e.name}
</option>)}
</select>
)
}
}
PureComponent PureComponent
class CountryDropDown extends PureComponent {
render() {
const { countrySelectionHandler, countryArray } = this.props
return (
<select onChange={countrySelectionHandler}>
{countryArray.map((e) => <option> {e.name}
</option>)}
</select>
)
}
}
PureComponent has shallow compare shouldComponentUpdate function by default. 默认情况下,PureComponent具有浅表比较shouldComponentUpdate函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.