简体   繁体   English

反应原生组件在渲染时不更新其 state

[英]React native component not updating its state while rendering

I am trying to render single book one by one from redux when click on next book button.当单击下一本书按钮时,我正在尝试从 redux 中逐一呈现单本书。 I achieved it by using below code but issue is when i try to add some book in bookmarks and click on next book then next book star also showed as orange (which is bookmarked state).我通过使用下面的代码实现了它,但问题是当我尝试在书签中添加一些书并单击下一本书然后下一本书星也显示为橙色(这是书签状态)。 I also tried to make a bookmark btn code as a component so that its current state wont show in the next book but its still showing.我还尝试将书签 btn 代码作为组件,以便其当前的 state 不会在下一本书中显示,但仍会显示。 How can i overcome this issue.我该如何克服这个问题。 Images of issue are attached.附上问题图片。 bookmarked state normal state书签 state正常 state

const renderBooks = totalBooks?.books?.length && totalBooks?.books[bookIndex] && <View>
    <View style={styles.spaceBetween}>
      <TouchableOpacity onPress={() => navigation.goBack()}><AntDesign name="arrowleft" size={28} color="#0b466b" /></TouchableOpacity>
      <Bookmark
        book={totalBooks?.books[bookIndex]}
      />
    </View>
    <ImageBackground source={totalBooks?.books[bookIndex].cover ? {uri: totalBooks?.books[bookIndex].cover} : BookCover} style={styles.bookCover}>

    </ImageBackground>
    <Text style={styles.bookTitle}>{totalBooks?.books[bookIndex].title}</Text>
    <ScrollView style={styles.bookDescriptionContainer} contentContainerStyle={{flexGrow: 1}}>
      <Text style={styles.bookDescription}>
        Lorem Ipsum is not simply random text. 
      </Text>
    </ScrollView>
    <View style={styles.actionBtnsContainer}>
      <TouchableOpacity onPress={() => setOptionsPopUp(true)}>
        <Text style={styles.buyItBtn}>Buy it</Text>
      </TouchableOpacity>
      <TouchableOpacity onPress={() => nextBook()}>
        <Text style={styles.readItBtn}>Next Book</Text>
      </TouchableOpacity>
    </View>
<View>


const [addToBookmark, setAddToBookmark] = useState(false);
  
  const handleBookmarks = () => {
    const myBookmark = addToBookmark;
    setAddToBookmark(!myBookmark)
    if(!myBookmark) {
      console.log('Add')
      addInBookmarks(book)
    } else {
      console.log('remove')
      removeFromBookmarks(book)
    }
  }

  useEffect(() => {

  }, [addToBookmark])

  return(
    <TouchableOpacity onPress={handleBookmarks}>
      <AntDesign name={addToBookmark ? "star" : "staro"} size={24} color={addToBookmark ? "#ff910c" : "#0b466b"} />
    </TouchableOpacity>
  );

In the function nextBook() you can try set the state of setAddToBookmark(false) or if the book as been marked the state could be set to false or true dependig your data在 function nextBook()中,您可以尝试将setAddToBookmark(false)的 state 设置为,或者如果这本书被标记为 state,则可以将您的数据设置为 false 或 true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 第一次单击时反应状态未更新,并且未渲染组件 - React State Not Updating on First click and Component Not Rendering 反应状态更新,但不渲染子组件 - React state updating but not rendering on child component 反应原生 redux state 更新但不在组件中呈现 - react native redux state update but not rendering in a component 如何在反应组件中呈现状态数组。 该数组在状态下可用,但在呈现其显示未定义时 - how to render the state array in react component. the array is available in state but while rendering its showing undefined 更新组件而仅渲染一次(反应) - Updating component while only rendering once (React) 渲染组件及其状态 - Rendering component with its state 在 React Native 中从子组件 TextInput 更新父组件状态 - Updating Parent component state from Child component TextInput in React Native React Native:依赖于 State 的样式正在更新,但未相应呈现 - React Native: Style Dependent on State is updating, but not rendering accordingly 在 state 挂钩中更新 object 时,React 组件不会重新渲染 - React Component does not re rendering when updating an object in state hooks 通过 useState 更新 state 后,React 组件未呈现 - React component isn't rendering after updating state by useState
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM