为什么警报没有停止?

时间:2021-04-30 23:47:02

标签: reactjs

import { ChildProcessWithoutNullStreams } from 'child_process';

const sendOutput = (output: ChildProcessWithoutNullStreams | string) =>
{
  if (typeof output === 'string') {
      console.log(`${output}\n`);
  } else {
    output.stdout.on('data', (data) => {
      console.log(`${data.toString()}\n`);
    });
    output.stderr.on('data', (data) => {
      console.log(`${data.toString()}\n`);
    });
}
}

export function helloWorldRepeat(){

  let dir = "/Users/les/projects/github/tutorial/les-app/cpp_program";
  let cmd = "helloWorldRepeat"

  let comm = dir+ "/"+cmd;
  let params = [""];

  return {
    command: `command: ${comm} ${params}`,
    process: spawn(comm, params)
  }

export function main(){
  let c = helloWorldRepeat();
  sendOutput(c.process);
}

}

材质 ui 的 <CardActions onClick={ this.state.latitude ? (async () => { await CustomDialog(<MapDialog points={[ { lat: this.state.latitude, lng: this.state.longitude }, { lat: item.provLat, lng: item.provLng } ]} />, { title: 'Routing', showCloseIcon: true, }); }) : alert("Please allow to access your location") <IconButton id='btn' className='button'>Show Path</IconButton> </CardActions> 组件中的此 <CardActions>。 当 {this.state.latitude} 为 false 时,为什么没有点击按钮就出现警报?

2 个答案:

答案 0 :(得分:2)

因为您应该将一个函数传递给您的 onClick 属性。在这里,您调用的是 alert,而不是传递函数。这样您的 alert 就会触发,而您的 onClick 不起作用,因为 alert() 会向它返回一个未定义的值。

所以你应该在你的三元运算符的第二部分传递一个函数,比如:

() => alert("Please allow to access your location")

答案 1 :(得分:0)

onClick 是一个事件处理程序,用于触发 functions,而不是简单地提醒消息,您应该在那里调用 function

<CardActions onClick={
         this.state.latitude ?
         (async () => {
                 await CustomDialog(<MapDialog
                        points={[
                               { lat: this.state.latitude, lng: this.state.longitude },
                               { lat: item.provLat, lng: item.provLng }
                        ]}
                  />, {
                  title: 'Routing',
                  showCloseIcon: true,
                  });
           })
           :
           (
             () => alert("Please allow to access your location")
           )
      <IconButton id='btn' className='button'>Show Path</IconButton>
</CardActions>

您的 onClick 方法看起来很匆忙,如果三元运算符返回 true,最好创建另一个函数并调用该函数。