How can I hide the badge when clicking notices button?

Viewed 97

I am getting some notices from 2 different api to show 2 different types of notices to the user. One is traffic notices and other one is more generic type of notices. So far the results are saved in the useState array and length of the each result is saved in the redux state.

When I click the notices button that has the badge i am trying to set the redux state to 0 but currently it doesn't work.

From this screen you get to

enter image description here

this screen here and from there you can go to one of the following

enter image description here

that is either these notices or traffic ones as seen in the image

enter image description here

What would I need to change to make it work?

In the home screen here I am currently receiving the results as follows


  const [noticesData, setNoticesData] = useState([])
  const [trafficNoticesData, setTrafficNoticesData] = useState([])

  const totalNotices = props.notices + props.trafficNotices
  const BadgedBtn = withBadge(totalNotices, { status: 'primary' })(
    TouchableOpacity
  )

  useEffect(() => {
    (async () => {
      let res = await fetch(
        'https://famkbzjos9.execute-api.eu-central-1.amazonaws.com/development/petodata'
      )
      let resData = await res.json()
      setNoticesData(resData)
    })()
  }, [])

  useEffect(() => {
    (async () => {
      let res = await fetch(
        'https://tie.digitraffic.fi/api/v3/data/traffic-messages/simple?inactiveHours=0&includeAreaGeometry=false&situationType=TRAFFIC_ANNOUNCEMENT'
      )
      let resData = await res.json()
      setTrafficNoticesData(resData.features)
    })()
  }, [])

  useEffect(() => {
    props.noticeActions.noticesAction(noticesData.length)
    props.trafficNoticeActions.trafficNoticesAction(trafficNoticesData.length)
  })

  const goToNotices = () => {
    props.noticeActions.noticesAction(0)
    props.trafficNoticeActions.trafficNoticesAction(0)
    navigation.navigate('Tiedotteet')
  }

        <View style={buttonGroup}>
          <TouchableOpacity
            activeOpacity={0.7}
            style={smallButton}
            onPress={() => navigation.navigate('Mainokset')}
          >
            <Text style={textStyle}>Tarjoukset</Text>
          </TouchableOpacity>
          {props.notices === 0 && props.trafficNotices === 0 ? (
            <TouchableOpacity
              style={smallButton}
              onPress={() => navigation.navigate('Tiedotteet')}
            >
              <Text style={textStyle}>Tiedotteet</Text>
            </TouchableOpacity>
          ) : (
            <BadgedBtn style={smallButton} onPress={goToNotices}>
              <Text style={textStyle}>Tiedotteet</Text>
            </BadgedBtn>
          )}
        </View>

const mapStateToProps = (state) => ({
  notices: state.noticesReducer.notices,
  trafficNotices: state.trafficNoticesReducer.trafficNotices,
})

const mapDispatchToProps = (dispatch) => ({
  noticeActions: bindActionCreators(noticesAction, dispatch),
  trafficNoticeActions: bindActionCreators(trafficNoticeAction, dispatch),
})

This is my action file


const TRAFFIC_NOTICES_SET_VALUE = 'TRAFFIC_NOTICES_SET_VALUE'

export const trafficNoticesAction = (value) => ({
    type: TRAFFIC_NOTICES_SET_VALUE,
    payload: { trafficNotices: value }
})

const NOTICES_SET_VALUE = 'NOTICES_SET_VALUE'

export const noticesAction = (value) => ({
    type: NOTICES_SET_VALUE,
    payload: { notices: value }
})

And the reducer is as follows

const TRAFFIC_NOTICES_SET_VALUE = 'TRAFFIC_NOTICES_SET_VALUE'

const initialState = {
  trafficNotices: 0,
}

const trafficNoticesValueReducer = (state = initialState, action) => {
  switch (action.type) {
    case TRAFFIC_NOTICES_SET_VALUE:
      return {
        ...state,
        trafficNotices: action.payload.trafficNotices,
      }
    default:
      return state
  }
}

export default trafficNoticesValueReducer
const NOTICES_SET_VALUE = 'NOTICES_SET_VALUE'

const initialState = {
  notices: 0,
}

const noticesValueReducer = (state = initialState, action) => {
  switch (action.type) {
    case NOTICES_SET_VALUE:
      return {
        ...state,
        notices: action.payload.notices,
      }
    default:
      return state
  }
}

export default noticesValueReducer
1 Answers

I don't know why you should use redux here. There is no need to use redux for this part.


const [noticesData, setNoticesData] = useState([])
const [trafficNoticesData, setTrafficNoticesData] = useState([])

const totalNotices = useMemo(() => noticesData.length  +trafficNoticesData.length  ,[noticesData,trafficNoticesData])
const BadgedBtn = withBadge(totalNotices, { status: 'primary' })(
  TouchableOpacity
)

useEffect(() => {
  (async () => {
    let res = await fetch(
      'https://famkbzjos9.execute-api.eu-central-1.amazonaws.com/development/petodata'
    )
    let resData = await res.json()
    setNoticesData(resData)
  })()
}, [])

useEffect(() => {
  (async () => {
    let res = await fetch(
      'https://tie.digitraffic.fi/api/v3/data/traffic-messages/simple?inactiveHours=0&includeAreaGeometry=false&situationType=TRAFFIC_ANNOUNCEMENT'
    )
    let resData = await res.json()
    setTrafficNoticesData(resData.features)
  })()
}, [])

const goToNotices = () => {
    setNoticesData([])
    setTrafficNoticesData([])
  navigation.navigate('Tiedotteet')
}

      <View style={buttonGroup}>
        <TouchableOpacity
          activeOpacity={0.7}
          style={smallButton}
          onPress={() => navigation.navigate('Mainokset')}
        >
          <Text style={textStyle}>Tarjoukset</Text>
        </TouchableOpacity>
        {noticesData.length === 0 && trafficNoticesData.length === 0 ? (
          <TouchableOpacity
            style={smallButton}
            onPress={() => navigation.navigate('Tiedotteet')}
          >
            <Text style={textStyle}>Tiedotteet</Text>
          </TouchableOpacity>
        ) : (
          <BadgedBtn style={smallButton} onPress={goToNotices}>
            <Text style={textStyle}>Tiedotteet</Text>
          </BadgedBtn>
        )}
      </View>
Related