无状态组件使用ref调用公共方法

时间:2017-03-16 06:45:31

标签: javascript reactjs

我在SomeCompoent中有一个公共方法。如何在其父母处调用它?

const firstDateComponent = ({ month, year, navigate }) => {
  const firstDate = new Date(year, month, 1).getDay();

  function getEmptyDays(firstDate) {
    const emptyDays = [];
    for(let i=0; i < firstDate; i++) {
      emptyDays.push(
        <li className='month-day empty' key={ `${year}${month}-empty${i}`}>.</li>
      );  
    }
    return emptyDays;
  }

  if(navigate){
    //call the clear method here
  }


  return (
    <div>
      <ul className='months'>
          <SomeCompoent ref={param => this.__selection = param}>
            { getEmptyDays(firstDate) }
          </SomeCompoent>
      </ul>

      <p onClick={e => this.__selection.clear()}>this work if call in jxs</p>
    </div>
  );
}

我设法通过使用ref在jsx中调用clear方法。但是如何在nagivate if语句中调用呢?

0 个答案:

没有答案