Jacse / react-native-app-intro-slider

Simple and configurable app introduction slider for react native
MIT License
1.66k stars 330 forks source link

State not managed. #188

Closed Saad-Bashar closed 4 years ago

Saad-Bashar commented 4 years ago

Having a weird problem, So I have an array state called answers , and on button press I just want to add new object to this state. I am doing it with spread operator but somehow the new object always overrides the objects inside array. It is not appending. Snippet of my code given below

questions = [
  {
    no: 1,
    key: '1',
    question: 'Your gum bleeds when you brush your teeth',
  },
  {
    no: 2,
    key: '2',
    question:
      'Your gum appears reddish and slightly swollen, with rounded edges at the areas between teeth',
  }]

const [answers, setAnswers] = useState([]);

<AppIntroSlider
        renderItem={renderItem}
        data={questions}
 />

in render item,

        <Button
          onPress={() => {
            setAnswers([...answers, {no: item.no, ans: 'yes'}]);
          }}>
          Yes
        </Button>
        <Button
          onPress={() => {
            setAnswers([...answers, {no: item.no, ans: 'no'}]);
          }}>
          NO
        </Button> 
Jacse commented 4 years ago

I don't think this is an issue with the component. Is the renderItem-function defined inside your component so it has access to the answers-state?

Jacse commented 4 years ago

Closing for inactivity.