使用 React Hook 导出布尔值

Export a boolean value with react hook

我有这个钩子,我需要导出一个带有布尔值的常量,这种方式是发送一个字符串。

export const useType = () => {
  
    const isFoo = 'string';
    const isBar = 'other_string';
  
    return { isFoo, isBar };
  };

如何将其转换为布尔值?

我是新手,如果有人能帮助我,谢谢

我不确定我是否正确理解你的问题,但你是这个意思吗?

export const useType = () => {
  
    const isFoo = 'string';
    const isBar = 'other_string';
    const iAmBoolean = true;
  
    return { isFoo, isBar, iAmBoolean };
  };

假设您需要检查一个值是否是等同于 is Foo 和 isBar 的字符串。


export const useType = (props) => {
 if(props === 'isFoo'){
 return true
}else{
 return false
}
}

//then you call useType(varWithTheString) to check if it's Foo or not.