Search code examples
react-nativereact-bootstrapreactstrap

Change Toast icon color conditional in React


I would like to change the icon os the toast component using a condition. I am trying do it using a ternary operator, but is not working. Can anyone help me please? I had the follow idea:

renderResultadoManifestacao(){
  var tag = '';
  const listItems = this.state.resultList.map((d) => 
  //<li key={d.name}>{d.name}</li>);
    <Toast>
      {d.success == true ? tag = "success" : tag = "error" }
      <ToastHeader icon = {tag}> {d.chaveAcesso} </ToastHeader>
      <ToastBody>
        {d.resultado.map((r) =>
          <li key={r}>{r}</li>
        )}
      </ToastBody>
    </Toast>
  );

  return (
    <div>
      {listItems }
    </div>
  );
}

Solution

  • You can set the tag variable inside the map's function.

    renderResultadoManifestacao(){
            const listItems = this.state.resultList.map((d) => {
                var tag = d.success ? "success" : "error";
                return <Toast>
                    {tag}
                    <ToastHeader icon = {tag}> {d.chaveAcesso} </ToastHeader>
                    <ToastBody>
                        {d.resultado.map((r) =>
                            <li key={r}>{r}</li>
                        )}
                    </ToastBody>
                 </Toast>
            });
    
            return (
                <div>
                    {listItems}
                </div>
            );
    
    }