【问题标题】:React Native hooks usage with multiple objectsReact Native hooks 使用多个对象
【发布时间】:2020-11-10 11:33:38
【问题描述】:

我正在尝试创建一个待办事项列表应用程序,该应用程序带有一个共享按钮,可以共享您拥有的待办事项列表。该应用程序几乎完成了,我认为代码的其他部分无关紧要,但如果需要我可以发布它们。

我的状态是这样的:

const [todos, setTodos] = useState([
    {todo: 'Add a todo', key: '1'},
    ]);

我的分享功能是这样的 - 直接取自官方文档 -:

 const onShare = async () => {
    try {
      const result = await Share.share({
        message:
          todos.todo
      });
      if (result.action === Share.sharedAction) {
        if (result.activityType) {
          // shared with activity type of result.activityType
        } else {
          // shared
        }
      } else if (result.action === Share.dismissedAction) {
        // dismissed
      }
    } catch (error) {
      alert(error.message);
    }
  };

我在哪里渲染按钮并调用函数:

<Button color= 'orange' title={'Share'} onPress={onShare}/>

我的问题出在message: 部分(第二个代码块),我无法在todos 状态内访问我的个人todo。这可能是最简单的问题,但我找不到方法......

请帮忙:)

【问题讨论】:

    标签: javascript reactjs react-native react-hooks


    【解决方案1】:

    试试这种方式,将所有待办事项的数据包装在字符串中

    const justTodos = todos.map(item => item.todo);
    const result = await Share.share({
            message: JSON.stringify(justTodos)
    })
    

    【讨论】:

    • 我尝试过这种方式,但它共享如下内容:** [{"todo":"my second task ","key":"0.5400277526099981","marked":false},{" todo":"我的第一个任务","key":"0.505473938112936","marked":false}] **
    • 我只想显示“待办事项”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-09
    • 2019-11-06
    • 2019-10-17
    • 2020-11-19
    相关资源
    最近更新 更多