Javascript render中的react调用方法

Javascript render中的react调用方法,javascript,reactjs,Javascript,Reactjs,如果this.props.models数组中只有一个项,我将尝试自动调用我的方法onModelSelect {this.props.modelCheck&&{this.props.modelCheck.map(item=>{()=>this.onModelSelect(item.id)}}} 如果我将该方法附加到onClick事件处理程序,它会工作,但就其本身而言,我不确定要使用的语法,因为它在组件呈现时不起任何作用 export default class App extends Compo

如果this.props.models数组中只有一个项,我将尝试自动调用我的方法
onModelSelect

{this.props.modelCheck&&{this.props.modelCheck.map(item=>{()=>this.onModelSelect(item.id)}}}

如果我将该方法附加到onClick事件处理程序,它会工作,但就其本身而言,我不确定要使用的语法,因为它在组件呈现时不起任何作用

 export default class App extends Component {
        onModelSelect = (modelId) => {
          this.props.selectModel(modelId);
          this.props.setModelSelected(true);
          console.log('test')
          console.log('modelId',modelId)
        }
        render() {

          return(
            <div>
                {this.props.modelCheck && <div>{this.props.modelCheck.map(item => {()=>this.onModelSelect(item.id)} )}</div>}
              {this.props.models.map(model =>
                <div onClick={()=> this.onModelSelect(model.id)}>Select Model</div>
              )}
            </div>
          )
        }
      }

      const mapStateToProps = (state) => {
        const modelCheck = getFilteredSelectableModels(state).length === 1 && getFilteredSelectableModels(state)

        return {
          modelCheck,
        };
      };

      const mapDispatchToProps = (dispatch) => {
        return bindActionCreators({
          ...settingDropActions,
        }, dispatch);
      };

      export default connect(mapStateToProps, mapDispatchToProps)(SettingDropModel);
导出默认类应用程序扩展组件{
onModelSelect=(modelId)=>{
this.props.selectModel(modelId);
this.props.setModelSelected(true);
console.log('test')
console.log('modelId',modelId)
}
render(){
返回(
{this.props.modelCheck&&{this.props.modelCheck.map(item=>{()=>this.onModelSelect(item.id)}}
{this.props.models.map(model=>
this.onModelSelect(model.id)}>Select model
)}
)
}
}
常量mapStateToProps=(状态)=>{
常量modelCheck=getFilteredSelectableModels(状态)。长度===1&&getFilteredSelectableModels(状态)
返回{
模型检查,
};
};
const mapDispatchToProps=(调度)=>{
返回bindActionCreators({
…设置删除操作,
},派遣);
};
导出默认连接(mapStateToProps、mapDispatchToProps)(设置DropModel);

为什么要将这一行添加到渲染方法中:

{this.props.models.length === 1 && this.props.models.map(model => ()=>this.onModelSelect(model.id))}
首先,应该使用react JSX中的
.map
来生成元素数组,这里没有这种情况。所以这个实现是不正确的

若只有一个模型,则要在ModelSelect方法上调用
,应在
componentDidMount
react生命周期方法中调用它

componentDidMount(){
    if(this.props.models.length === 1){
        this.onModelSelect(this.props.models[0].id);
    }
}

当放入
componentDidMount()
时,它不起作用,但当放入
componentWillReceiveProps()时,它确实起作用


该行出现错误,我将尝试将其移动到componentDidMount。当您移动到
componentDidMount
时,它肯定会工作。是的,这意味着在首次渲染组件
时,此.props.models
不可用。因此,您必须将其写入
组件willreceiveprops
。但是有一个解决办法:您应该使用
nextrops
而不是
this.props
component中将接收props
。理想情况下,您应该在两个函数中都编写它。
componentWillReceiveProps(nextProps) {
    if(this.props.models.length === 1){
      this.onModelSelect(this.props.models[0].id);
    }
  }