undefined问题的处理


import React, { Component } from 'react'
// 将对象转换为Url的params形式 // let obj = {name:'tom', age:18} // const params = new URLSearchParams(obj); // console.log(params.toString());
// 将Url的params转换为对象 // let str = 'carName=奔驰&price=10000'; // const params = new URLSearchParams(str); // console.log(params); // console.log(params.get('carName')); // console.log(params.get('price'));
export default class Detail extends Component {
  DetailData = [     {id:'01', content:'你好,中国'},     {id:'02', content:'你好,尚硅谷'},     {id:'03', content:'你好,未来的自己'},   ];
  render() {     console.log(this.props);
    // // 接收params参数     // const {id, title} = this.props.match.params;
    // // 取url参数     // const { search } = this.props.location     // // 将截取后的字符串对象化     // const searchParams = new URLSearchParams(search.slice(1))     // const id = searchParams.get('id')   // 12431243     // const title = searchParams.get('title') // 2
    // 取state参数     const { id, title } = this.props.location.state || {}     const findResult = this.DetailData.find((detailObj)=>{       return id===detailObj.id;     }) || {};     return (      
           
  • Id:{id}
  •        
  • Title:{title}
  •        
  • Content:{findResult.content}
  •      
    )   } }