user15322469
user15322469

Reputation: 909

How can I change the alert string based on conditions?

There are times when nicknamesuccess comes out as a string in useSelector and there are times when nicknameError comes out as a string. If either is a string then one is null.

both initial values ​​are null.

What I'm trying to do is render an Alert.alert when either comes in as a string.

how can i fix my code?

this is my code

    const Nickname = () => {

      const {
        nicknameError,
        nicknamesuccess,
      } = useSelector((state) => state?.user);

      const createTwoButtonAlert = () =>
      Alert.alert('중복확인',`${nicknameError}` ||`${nicknamesuccess}`, [
        {text: 'OK', onPress: () => console.log('OK Pressed'), style: 'OK'},
      ]);


      return (
        <Container>
          <NicknameContainer>
            <Inputs placeholder="닉네임" value={nick} onChange={onChangeNick} />
          </NicknameContainer>
        </Container>
      );
    };

    export default Nickname;

if i use my code it doesn't work it render null or undefiend....

Upvotes: 1

Views: 78

Answers (2)

Ayaz
Ayaz

Reputation: 2121

Here template string is not required. You can just use the variables.

const createTwoButtonAlert = () =>
      Alert.alert('중복확인', nicknameError || nicknamesuccess, [
        {text: 'OK', onPress: () => console.log('OK Pressed'), style: 'OK'},
      ]);

Upvotes: 1

Thanos T.
Thanos T.

Reputation: 182

This should fix your problem:

`${nicknameError ? nicknameError : nicknamesuccess}`

If nicknameError has a falsy value (fale, null, 0 , '') then the code above (ternary operator) returns nicknamesuccess else it returns nicknameError

Upvotes: 1

Related Questions